"That" is used with restrictive phrases. Restrictive phrases are phrase. that are essential to the sentence.
"Which" is used with nonrestrictive phrases. Nonrestrictive phrases are phrases that state non essential information. A phrase is considered nonrestrictive phase if the phase can be omitted from the sentence.
Examples:
* The shirt that you lent me is in my bag.
* The shirt, which is red, is in my bag.
* The house that I wanted to buy has been sold.
* The house, which I didn't want to buy, has been sold.
* The food store that I go to all the time is closed today.
* The store, which is near my house, is not open today.