But is a conjunction.
But as a linking word
We use but to link items which are the same grammatical type (coordinating conjunction). But is used to connect ideas that contrast.
The phrase but not is common:
The room has been painted but not in the colour that I asked for.
I’d love to go for a pizza with you but not tonight.
But meaning ‘except’
But means ‘except’ when it is used after words such as all, everything/nothing, everyone/no one, everybody/nobody:
The cleaning is done now, all but the floors. They still have to be washed.
I arrived at the airport and realised that I’d brought everything but my passport!
Everyone but Anna has checked in.
Nobody but the receptionist was left in the lobby of the hotel.
We use object pronouns after but (me, you, him, us, etc.) even in subject position:
Everybody but me has paid.
No one but him would get a job like that.
In formal situations, we can use subject pronouns after but:
Everyone but she knew how the drama was going to end.
But for + reason
But for is used to introduce the reason why something didn’t happen:
But for the traffic, I would have been here an hour ago. (The traffic was very heavy – if it weren’t for the traffic, I’d have been here an hour ago.)
They would have been badly injured but for the fact that they were wearing seat belts. (They were wearing seat belts – if it weren’t for the fact that they were wearing seat belts, they would have been badly injured.)
All but meaning ‘almost completely’
I had all but finished the essay when the computer crashed and I lost it all.
His parents had all but given up hope of seeing him again.