Both. The USB controllers know that there is more power (though not necessarily more than USB spec in the case of the iMac), and the keyboard knows that it can act as an intermediary and request more power on behalf of peripherals that are attached to its ports.
How Apple specifically implemented this, I haven't seen. Somehow or another, however, the power requests of the device are added to that of the keyboard (either by the keyboard itself doing the math, or by piggybacking all three requests back to the computer, or some plan C I haven't thought of), and the iMac responds.