They use an optoisolator. This is an LED and a phototransistor in one package. The mains drive the LED (current limited by resistors, of course) and the phototransistor can be used on the low voltage side to do whatever. Frequently, it just acts as a pulldown in concert with a resistor pull-up.