Is the universal force-shutdown mechanism built in at a hardware or firmware level?
Both the motherboard (hardware) and the BIOS (firmware) are involved in the process.
The actual switching of the power (on and off) is done by circuits on the motherboard. The circuits are triggered to change state by a momentary short circuit of two points in the circuit. That is done by the front panel push button. A short-term connection of that button is enough. The motherboard circuit also has a second and third function it can do.
The BIOS usually is set so that, if the circuit is already on the ON state and the momentary contact is kept closed for at least 4 seconds, one of two actions will be carried out. Either the system will shut down right away (like turning off a switch), or the motherboard will enter the "sleep" state and wait to be re-awakened. Which of these two options is done is something you set in the BIOS Setup screens.**
This is the reason that the motherboard is never entirely "Off" in most operating situations. Even when it is "Off", there is a small portion of it devoted to checking for inputs from a few sources (like the front push button, some USB devices and maybe a modem) so that it can respond by turning power on to the entire system. The way to really shut down all power is either to pull out the power cord, or to use the physical switch mounted on the back of the PSU to switch it off.
Source How (does the) power button work?
The modern switches don't contain any magic. In fact, they are
less complicated and expensive than real physical on/off
These switches are just inputs to a micro-controller. The
micro controller can tell when you push the button, and the rest is
policy encoded in the firmware to decide what to do about it. The
power is usually switched with transistors. This means the button
itself doesn't have to handle high voltage or high current, so there
are a lot more options to make it and for it to be small. It could be
a membrane switch, for example, which you'd never use to switch wall
This does mean that a little bit of the device is usually on, at least
enough to power the micro-controller. However, modern micro-controllers
can take such tiny amounts of power when doing nothing but waiting for
a switch signal that this power is irrelevant in most cases.
In some cases, the button actually causes the micro to get powered up
when pressed, which then turns on some transistors or a relay or
something to keep the power on. When you press the button to turn the
device off, the micro shuts down everything, including itself.
Source How do these modern power buttons on devices work answer by Olin Lathrop