With relative clauses:
A relative clause is a phrase within a sentence that modifies a noun and starts with that, which, or who/whom/whose. Sometimes a relative clause contains information essential to identifying a specific person or thing, but other times it contains extra information. For example:
When we visited the mother dog and her litter, we decided to take the puppy who had a white spot on her chest.
Our puppy, who has a white spot on her chest, loves to play fetch.
Notice how in the first example, the relative clause who had a white spot on her chest is essential for identifying which puppy we selected. If you got rid of that clause, the remaining sentence wouldn’t make any sense: *When we visited the dog and her litter, we decided to take the puppy. This leaves the reader wondering which puppy we picked. (BTW, the asterisk before the sentence means that it’s ungrammatical.)
In the second sentence, however, since we don’t have to single out a particular puppy, you could leave out the relative clause who has a white spot on her chest and the sentence would still make sense: Our puppy loves to play fetch. In this case, the clause who has a white spot on her chest is extra information.
Traditionally, grammarians have called the essential type “restrictive” and the extra type “non-restrictive.” But I’ve never found those terms helpful, so I’m going to stick with these concepts of essential and extra information throughout this post.
Now, on to the role of the comma. Look again at the essential example:
When we visited the mother dog and her litter, we decided to take the puppy who had a white spot on her chest.
Note that there is no comma before who. But in the extra example:
Our puppy, who has a white spot on her chest, loves to play fetch.
There are commas around the clause who has a white spot on her chest. So the rule is: if the relative clause contains essential information, it does not take commas; but if the relative clause contains extra information, it does take commas.
Another way to look at it is that the commas are functioning almost like parentheses—they are signposts that mean “We don’t need this information to identify someone or something.”
One more example:
Every book that Chuck owns is about sports.
The book The Boys of Summer, which Chuck owns, details the history of the early 1950s Brooklyn Dodgers.
If we remove the essential relative clause that Chuck owns from the first sentence, we’d be left with Every book is about sports. And of course that’s not true. Notice that there are no commas, either.
But we can remove the extra relative clause—everything between the commas—from the second and it’s still a true statement: The book The Boys of Summer details the history of the early 1950s Brooklyn Dodgers. The fact that Chuck has a copy of this book is extra information.
You’ve no doubt noticed that in the first sentence the word that starts off the relative clause, but in the second it’s which. You’ll come across some traditional grammars that prescribe using that only with essential clauses, and which only with extra clauses. But this is not actually a hard and fast rule. Native speakers of English tend to automatically use that only with essential clauses, but you can use which with either type. Just go with whichever sounds better to your ear, and then check whether you need to have commas (extra) or not (essential). Chances are that if you naturally used that, it will be an essential clause, which does not need the commas.