But is a conjunction.
That is a type of word that helps connect different ideas and parts of a sentence. A simple way that you can remember most of the most common conjunctions is to use FANBOYS.
Each letter stands for one of these conjunctions: for, and, nor, but, or, yet, so. They join sentences or phrases with different ideas together in different ways, but the comma usage is always the same.
If you see one of these conjunctions in a sentence, you should always put the comma BEFORE the conjunction!
Unless you have a qualifying phrase (a phrase that provides more information about the sentence that follows), you should have a comma before the FANBOYS. For example:
- I had originally planned to go to the carnival on Saturday, but my friend had somewhere to be so we cancelled the plan.
- No one expected Thomas to show up at the meeting today, but, lo and behold, there he was, prepared to give his speech! (Note that the phrase here, lo and behold, has a comma both before it (after but and after it)