A comma is generally used to separate two or more independent clauses in a compound sentence that is joined by a coordinating or, occasionally, a correlative conjunction.
The children learned to ride their bicycles, and then they rode them in the park.
I want to see that new movie, but I am too late.
Sammi wants to get a new haircut, so we are going to the salon on Friday.
If you're not sure whether or not the sentence needs a comma, check to see if each half of the sentence makes sense by itself. You can test whether each half makes sense by replacing the conjunction with a period. If each half does make sense by itself, you need to add a comma before the conjunction. Compound subjects and compound predicates are not separated by commas.
I visited my sister in London and had a wonderful time.
This sentence doesn't need a comma because the second half of the sentence can't stand alone: had a wonderful time isn't a complete sentence because the subject is missing. This is an example of a compound predicate (visited my sister + had a wonderful time), which doesn't require a comma.
I meant to return that book to you but forgot it on the kitchen table.
Just like the previous sentence, this one doesn't need a comma because forgot it on the kitchen table is missing the subject and doesn't make sense by itself.