How does the squat switch work?
The squat switch is essentially a sensor that senses if the weight of the aircraft is resting on the gear. This can be done by load cell or displacement sensor on the suspension arm.

Like any other component these sensors can fail though engineers prefer them to fail in an always on (weight on the gear) state; that way the pilot just needs to deal with gear failing to retract after takeoff and needing to land again to check it out rather than dropping the plane on the ground and causing more damage.