That and which are technically interchangeable in many contexts, but there are unwritten rules that tend to guide their use. The two main unwritten rules are: first, if you can use that, it’s usually better than which. Second, when you use which, it should follow a comma. These are not unbreakable rules, though, and exceptions abound.
Restrictive and nonrestrictive clauses
That is generally more useful for introducing restrictive clauses (clauses that give essential information about the preceding noun), and which is more useful for introducing nonrestrictive clauses (clauses that give nonessential information). Consider this example:
House Republicans scrambled Friday to finish work on a bill that will keep the government open beyond March 4th. [CBS News]
Without the information introduced by that, the sentence would be vague and confusing. Consider the alternative:
House Republicans scrambled Friday to finish work on a bill, which will keep the government open beyond March 4.
The that version informs us that the bill pertains to keeping the government open. Read literally, the hypothetical which version says that the bill happens to keep the government open, but we’re left in the dark about what the bill is actually about.
Let’s look an opposite example:
The Roman Catholic Church, which hasn’t always seen face-to-face with modernity, has embraced at least one product of the digital age. [New York Times]
This sentence presents the Church’s spotty record of seeing face-to-face with modernity as an inessential detail. Consider the alternative:
The Roman Catholic Church that hasn’t always seen face-to-face with modernity has embraced at least one product of the digital age.
This version of the sentence would lead us to believe there are two Roman Catholic churches, one that has seen face-to-face with modernity and one that hasn’t. The meaning of the sentence would be utterly changed.
Commas
Because which is preferred for introducing nonrestrictive clauses, and because nonrestrictive clauses are usually set off from their sentences with commas, which is usually preceded by a comma.
Still, there is no rule against using which in place of that to introduce nonrestrictive clauses, though this goes against the fairly well-established modern convention. For example, these writers use which where most others would likely use that:
He testified to Banton’s connection to the drug deal which resulted in the artiste’s arrest. [Jamaica Observer]
But, for that to happen, he needs to come out of hiding and address the issues which need to be addressed. [The Province]
Some careful English writers might recommend changing these whiches to thats, but there’s no logical reason for such an all-encompassing rule, and which is so often used this way, and has been for centuries, that we can’t consider it incorrect.