When to use “that” and when to use “which”, especially in relative clauses
That and which are interchangeable when introducing integrated relative clauses. Although some grammar mavens (i.e., people who hold forth on such topics but know little or nothing about linguistics) and copy editors will insist otherwise, the rule is completely bogus.
See, for instance, Language Log on that vs which, written by the co-editor of The Cambridge Grammar of the English Language.
Practically speaking, it is not something that any normal person will generally notice or follow in spoken English and it's frequently — and rightly — ignored even in literary writing. So even from that point of view it's not worth worrying about.
Well, the difference is slight but real. According to the New Oxford American Dictionary:
In U.S. English, it is usually recommended that which be employed only for nonrestrictive (or nonessential) clauses: : the horse, which is in the paddock, is six years old (the : which clause contains a nonessential fact, noted in passing; the horse would be six years old wherever it was). A : that clause is restrictive (or essential), as it identifies a particular thing: : the horse that is in the paddock is six years old (not any horse, but the one in the paddock).
Note also that the word that can be omitted where it introduces a subordinate clause:
He said he was coming. [He said that he was coming.]
But it is required when it is the subject of the clause:
The family that plays together stays together.
More usage notes from NOAD:
Is there any difference between the use of that and which in sentences such as : any book that gets children reading is worth having, and : any book which gets children reading is worth having? The general rule is that, in restrictive relative clauses, where the relative clause serves to define or restrict the reference to the particular one described, that is the preferred relative pronoun. However, in nonrestrictive relative clauses, where the relative clause serves only to give additional information, which must be used: : this book, which is set in the last century, is very popular with teenagers, but not : this book, that is set in the last century, is very popular with teenagers.
Actually, there's more to this than mentioned in some other answers. The word that is a subordinator; it is not a relative word like who, where, when, or which. Even in integrated relative clauses, they are not always interchangeable. When the relative construction follows a fronted preposition, only relative words will do, so relative pronoun which is available, but that isn't.
We have to protect the environment in which we live.
No art can be properly understood apart from the culture of which it is a part.
Conversely, when the relative clause is post-modifying superlatives, we can choose between that or no subordinator, but which is not possible:
He's the best (that) I've ever seen.
He's the fastest runner (that) I've ever seen.
Also in cleft sentences with prepositional phrases like the following, only that is available.
It wasn't for you that I bought it.
It was from John that she heard the news.
Finally, which usually cannot be used where other relative words would work, but that typically can:
They gave the prize to the girl that spoke first. [who]
He was to leave at the time that she arrived. [when]
They looked every place that she could be. [where]
That's not the reason that she resigned. [why]
I like the way that she plays. [*how]
Generally, "that" goes with restrictive clauses - those where the information provided in the clause is necessary to identify the subject: "The beer that belongs to me" (as opposed to all other beers in the world).
"Which" goes with non-restrictive clauses - those which give information but which do not define the subject: "The beer, which was a little warm, was still tasty."