A comma should be used before “which” in a sentence when the clause that follows is nonrestrictive (meaning it provides additional, nonessential information). For example, “The book, which was written by my favorite author, is on the shelf.” A comma should not be used before “that” in a sentence, as “that” is used for restrictive clauses (meaning the information is essential to the meaning of the sentence).