To understand the distinctions between that and which it is necessary to understand restrictive and nonrestrictive clauses.
Learning these distinctions is one technical aspect of grammar that every user of English should understand, because it is at the root of an assortment of grammatical errors.
A nonrestrictive, or nondefining, clause is one that can be regarded as parenthetical:
My house, which has a blue door, needs painting.
The italicized words are effectively an aside and could be deleted. The real point of the sentence is that the house needs painting; the blue door is incidental.
Use commas to set off nonrestrictive elements, which contribute to, but do not determine, the meaning of the sentence. These elements may be clauses (groups of words that contain a subject and a verb) or phrases (groups of words that do not contain both a subject and a verb).
A restrictive, defining, clause is one that is essential to the sense of the sentence.
My house that has a blue door needs painting.
Here the blue door is a defining characteristic, it helps to distinguish that house from my other houses.
Restrictive clauses or phrases are not separated off with commas. A restrictive clause or phrase is essential to the meaning of the sentence; it defines the word it modifies by "restricting" its meaning. Eliminating a restrictive element from a sentence changes its meaning dramatically.
Note that nonrestrictive and restrictive clauses must be introduced by the appropriate relative pronoun.
In correct usage that is always used to indicate restrictive clauses and which to indicate nonrestrictive ones.
Restrictive clauses should NEVER be set off with commas and
nonrestrictive clauses ALWAYS should.
On that much the authorities are agreed. Where divergence creeps in is on the question of how strictly the distinctions should be observed.
Today, that is more usual in short sentences or early on in longer ones.
The house that John built.
Which often appears where that would more strictly be correct, particularly in Britain.
Americans, in contrast, are much more inclined to use that where which might be preferable.