We use which when we have a doubt in specifying or emphasising a method of asking a question. For ex. He has a pencil. Which colour pencil?- Here we used which to ask the colour of the pencil.
We use that when we are one hundred percent sure of what you want to emphasise. For ex. Ram:- He has a pen.
Pam:- I know. This one is blue while that one is black.- Here we used which as Pam knew which pencil and where. We also use that when there is something we are sure of but is at a distance from us.