What does STONITH do at the OS level?

When you do “Shoot the other node in the head “- what exactly is executed on OS level?

The exact process depends on the STONITH device. For SBD, disk based fencing, pacemaker writes a poison pill to the slot of the node to be fenced and the node takes the pill and dies.