Disclaimer: The rule I am about to explain has not always been the rule—it is, however, the current, generally accepted way to handle which vs. that.
Which and that both introduce dependent clauses (that is, a group of words that on its own does not create a complete sentence).
Which introduces clauses that are unnecessary to complete the main idea of the sentence, but rather add on more information; these are always set off with a comma and followed by a comma if necessary:
She ate an apple, which was red.
That introduces clauses that contain necessary information to fully convey the main idea of the sentence and are not set off by commas:
She spent the money that her grandmother gave her on balloons and candy.
Which = nonessential, use commas
That = essential, no commas
Pretty straightforward, right?