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.
bicycles.stackexchange.com/users/1259/zenbike
They quite simply must sway the bike back and forth because of the mechanical reality of the situation.
It's not even really a conscious act — if the rider didn't do this, the bike would fall out from under them.
These riders are applying extreme power to each pedal.
Since the pedals are not centered laterally, applying a large force to the right pedal (for instance) will, physically speaking, apply a rotational force that pushes the top of the bike to the right and the bottom of the bike to the left.
Without this counterbalancing motion, they would quite literally kick the wheel to the side out from under them.
By swaying the bike in the opposite direction, they increase the amount of force that can be applied to the pedals without crashing.
Luckily, it's an intuitive motion that happens completely automatically to any rider in that situation.
For a quick mental picture, imagine somebody swaying in the same direction as the pedal being pushed.
For instance, somebody leaning the bike to the right while they apply a large force to the right pedal.
The rotational force would rotate the bicycle clockwise, lifting the wheel off the ground.
Not something you want to happen, especially at sprinting speeds.
bicycles.stackexchange.com/users/1442/Stephen Touset
I've (anecdotally) noticed this effect is much more pronounced on road bikes with drop bars than other styles of bikes.
It is quite difficult to prevent on a road bike when standing and pedaling hard; although it has little impact on performance or speed.
In my opinion the wobble is mostly a side-effect of holding tightly to the bars while standing and pedaling.
When you're really crushing the pedals you use your arms to essentially pull yourself down onto the bike and to maximize power to the pedals (if you weren't holding on firmly you would be lifting yourself above the bike rather than pushing the pedals around).
When pushing the pedals and pulling the handlebars it's quite difficult to prevent the bars from being turned slightly side-to-side, which results in part of the wobble.
Add to that most of your body weight being rocked from side to side and the bike begins to get that lean effect.
Overall it is quite difficult to prevent, and preventing the wobble would likely cost more in terms of speed than just compensating for it.
bicycles.stackexchange.com/users/1373/STW
When I first began to bunch ride with the boys we would have a sprint here and there.
I would hold the handlebars firmly so that the bike was as stable as possible.
One of the guys pointed out that this actually takes effort on my part and wastes energy.
Also the wobbling is designed to occur such that the downstroke on the pedal occurs when that pedal is directly under the rider and hence delivers the maximum power to the stroke.
bicycles.stackexchange.com/users/6690/felicity
@STW has the right idea, but I think it's worth noting that to the extent possible, you're trying to hold your upper body still, and (especially when sprinting)
you "wobble" the bike under you to maximize power.
In other words, most of the "wobbling" involved is less of an accidental side effect than something they actually work at.
Conversely, when you see somebody whose upper body is moving a lot, while the bike stays relatively upright, it's a pretty good sign that they're running out of energy.
There are some other (also intentional) reasons as well.
First, working your way through a tight peloton can be tough, especially close to the finish.
Most of the riders are staying as close together as they can to get maximum drafting.
Worse, unless they happen to be your team-mate, the people right next to you will often try to "squeeze" you to intimidate you enough to slow you down a tiny bit.
Here you're literally "throwing your weight around" to make it harder for others to squeeze you in.
Likewise, the "smoother" you ride, the easier it is for somebody to draft off of you.
The more you're moving sideways, the harder it is for them to get/stay in your draft.
Earlier in a race (like in a breakaway)
you'll see people do relatively quick sideways movements of the bike to help drop people drafting off of them.
In the final sprint, you usually can't afford to do that, but you're still doing the best you can to make drafting difficult, as long as you don't slow your self down (much) in the process.
Finally, if you're riding smoothly, it's fairly easy for somebody to stay close beside you and stay in your draft almost until they're in front of you.
By moving around as much as possible, you force them to pull farther to the side before they can pass safely, which means they lose your draft and have to do more of their own work to pass.
bicycles.stackexchange.com/users/394/Jerry Coffin
In all pro-cycling races, they start wobbling towards the finish line instead of biking steadily (e.g., in this video:  http://youtu.be/wJE69beIIoI ). Why do they do that?  Note: I have no experience with racing or a bike with handles curved to the front, so maybe a stupid question.
Why do racing cyclists wobble when furiously pedalling?
bicycles.stackexchange.com
<racing><technique>
bicycles.stackexchange.com/questions/5050
bicycles.stackexchange.com/users/None/
There are generally two reasons why cyclists wobble when pedalling furiously. The first, accidental and unconscious, is a result of the energy being input into the pedals. The second, however, is a conscious effort to stop other riders from passing them or riding intheir drift: it is easier to pass a cyclist rising smoothly than one wobbling madly. This can be a natural consequence of the strong and unbalanced forces involved in sprinting. The benefit of reducing this would be too minor, while costing riders some of their speed. This can also be a deliberate action that maximises the force on the pedal at each stroke, or makes it more difficult for riders behind to draft or overtake.
Sprinting cyclists are putting all of their energy into pedaling, and this naturally causes swaying due to the unbalanced forced involved. Reducing this would be difficult and not save enough energy to be worth it. Wobbling is actually a way to maximise power to the pedals on each stroke, and is a deliberate skill. This erratic movement takes up space and makes it more difficult for riders behind you to draft or overtake.