There are several rules at play -- most of them flexible.
#1: If "but" joins two complete clauses that could stand on their own as sentences, use a comma.
"I like apples, but I don't like oranges." BUT "I like apples but not oranges."
#2: If something is interjected parenthetically after the "but," then the commas around that insertion will mean there's a comma right after your but.
"I like apples but, assuming you're also interested in my feelings on citrus, not oranges."