No one has answered this completely here yet, so I will attempt to: the semantics are somewhat complex but can be understood in due time.
A restrictive clause is a phrase that contributes to the essential meaning of the sentence. For example, in the sentence "The painting in the hall that was blue was beautiful," "that was blue" is essential to the meaning of the sentence (there may be multiple paintings in the hall, wherefore it is necessary to include the distinguishing description) and therefore a restrictive clause. Contrarily, if there were only one painting in the hall, then this description would be unnecessary, and we would instead say, "The painting in the hall, which was blue, was beautiful," wrapping our non-restrictive clause inside a comma sandwich.
The words "which" and "that" are not always interchangeable. You may use "that" only with restrictive clauses and "which" with both restrictive and non-restrictive clauses (though it is generally more common to use "that" with restrictive clauses in American English). So the sentence "The painting in the hall which was blue was beautiful" is grammatical, whereas the sentence "The painting in the hall, that was blue, was beautiful" is not, for there is a comma sandwich setting off the clause "that was blue" and rendering it non-restrictive.
If you are not completely sure whether a certain clause is non-restrictive, just make it restrictive anyway, using, in accordance with your personal preference, either "that" or "which."
For example, consider a brown-furred dog running across the street. All of the following sentences regarding such a dog would then be grammatical, though emphasizing slightly different things:
"The dog that was brown ran across the street." (emphasizes that the dog is brown)
"The dog which was brown ran across the street." (still emphasizes that the dog is brown)
"The dog, which was brown, ran across the street." (sets the color of the dog as a casual aside, unnecessary to the central meaning of the sentence)
"The brown dog ran across the street." (the best, most concise version, for using "that" or "which" adds unnecessary verbose to the sentence)