How does the LEGO Super Mario figure know which power up is attached?
The Mario element has six switches on the front:

The inside of the pants in the power-up sets include six plastic ridges that can be either high or low. They interact with the switches on Mario's torso in much the same way as the cuts on a key interact with a particular lock. The torso reads the pattern on the buttons to determine the power-up state. Here's an example showing the six ridges on the regular pants from the starter set and the ridges on Cat Mario:

The switches expect the following states for each of the pants elements (bits are read from left to right when looking at Mario):




Bit Pattern
Power-up




000000
None


001100
Propeller


010001
Cat


010010
Fire


100001
Normal


100010
Builder




Power-ups can be emulated by holding in the correct buttons. The play experience won't be great, but this could be used to see what a power-up does if you don't own it.
There's more detail on exactly what is read and presented via the Bluetooth interface on Shinichiro Oba's Github.
It's worth noting that the included hats are for decoration only. They have no impact on behavior, and there is no mechanism for them to be detected or identified.