The heating element has three layers - innermost, a wire that gets hot. Around that, a ceramic insulator. And outermost, a metal cover to protect the ceramic insulator. Then the heating element is mounted in a small water tank. And that, along with some other components, is put into a suitably waterproof outer plastic box. The insulation is of course all tested at the factory.
The tank, heating element outer, and all the pipework in the general vicinity is then grounded, and the power supply passes through an RCD (our equivalent to a GFCI). There are also two or three temperature sensors, and a water pressure sensor.
Installers also have to have an up-to-date electrical safety license - so for they know better than to (for example) compromise the waterproofing of the enclosure by adding extra holes to make the wiring simpler.