I know this is old, but the answers here aren't the full story.
First, there is a 'static power consumption' component that is a resting state product of input voltage and total leakage current. Leakage current is, well, just what it sounds like. CPUs are these complicated surfaces with all kinds of conducting and nonconducting features and, when you apply voltage - like applying pressure to water in a complex system of pipes - some stuff is just going to leak. It is not an 'accident' necessarily when leaks happen, some of them are just entirely unavoidable even with today's technology, especially now that we are in quantum scales for fab processes like 3nm.
But more interesting is the transient power consumption
Transient power consumption PT can be calculated from PT = Cpd×VCC^2
×fI×NSW where fI= input signal frequency, NSW = number of bits
switching, and Cpd= dynamic power-dissipation capacitance. In the case
of single-bit switching, NSW = 1.
The formula simplifies things a lot in quantum-scale land. But, it is the most direct and accepted answer to your question in a 'classical physics/integrated circuits 201' textbook sense, if we're talking about the CPU in particular. And the part that isn't described well here so far is the power being a function of frequency and switching. Really, just switching, because frequency lends itself to faster switching.
I'll try to make an analogy here because I like analogies I'm sorry if it sounds childish but I just like analogies.
Each transistor has a bucket and an agent (you or a friend). Moving the electron liquid within, like others have said, generates heat, period. That is just a magical property of moving our bucket of electrons through the ether no matter how small or large the distance is. You also have a special ('gate') bucket that you just look at. When the gate bucket is full (sort of like a binary 1), you see that and know you must 'do your job' with the electron soup you possess (or, may not possess).
Your job can vary, and it really only depends on what is on your left and your right. You may have an infinite electron source on your left and another transistor agent on your right, who wants you to fill his own bucket. Or, you might take the contents of another agent's bucket on your left and fill or empty someone else's special 'gate' bucket to send them a message to 'do their job' (this and more complicated interactions with the gate is the fundamental thing that makes computation on silicon possible and much more interesting than just resistors and capacitors).
So lets say you play that last role of telling another agent to either do his job or not. You have filled another agent's gate bucket. Now you get a message to tell him to stop doing his job. The only available option is to dump the bucket in a drain on the floor, where it moves through a long series of waste pipes and creates more heat.
So as long as everyone's gate bucket isn't changing, everyone is full or has already been instructed to empty their buckets or hand it to the next guy and there is no moving liquid. None going from agent to agent, coming out of the wall, or being dumped into the sewer. So, no heat. Depending on how all of our transistor agents are arranged though, just one incoming gate bucket changing can result in a new message propagating to tens, thousands or millions of other agents telling them to fill or empty their buckets or pass them on to the next guy. That's a lot of liquid moving and a lot of heat.
This is why your CPU can use very little power (relatively) even when it is at full turbo frequency, if it doesn't have a load. A full load of data is not being calculated, bits are not being flipped, so not a lot of heat is being generated. Except that which comes from leaky buckets or components that don't fit this analogy. looking back at that formula the transient power is zero if either frequency or the number of changing bits is 0. In either case, nothing is changing so no liquid is moving to generate heat. I'll note here that this heat is many many orders of magnitude larger than the theoretical heat limit of information change from information theory.
If you find the analogy worthless just look at this more academic pdf
https://www.ti.com/lit/pdf/scaa035&ved=2ahUKEwjVhaOLqKT8AhWDkmoFHYErDD0QFnoECBoQAQ&usg=AOvVaw20JRuAUK3tt9XyfLSzTwuf