Not a stupid question. The simple answer is that they are throwing every ounce of leverage, weight, and power into the pedals and that much movement side to side is the visible result of trying that hard to move forward.
If you could stay absolutely still, and input the same amount of force to the pedal, then more of that energy would go to moving forward, but you can't. It's a matter of balance and leverage.