It's less magical than it sounds, but a lot of work goes into it. Depending on the system, hardware can partially wake at fixed intervals to check for incoming traffic, and fully wake to process that traffic.
Notably, most of the stack can stay asleep almost all the time, and wake up only when actual traffic is coming in.
Application-level polling can eat up the power savings as the whole hardware stack must wake up and process an application-level transaction taking anywhere from several tens of milliseconds to several full seconds. Compared to the "napping" state, this eats huge amounts of power.