Does rocking the bike side to side when e.g. sprinting or climbing help?
The rocking motion does not help with power output, but it is the way you maintain balance with least effort.

When one is riding in straight line, the system of bike and rider rotates almost freely around the axis that goes through the points where tires contact the road (there are steering forces to maintain the balance, but these are very small compared to pedaling forces). Because there is nothing to brace against, the torques around this line have to even out. Because pedals are not directly above the line, pushing them down needs some torque to counter it. When sitting down, the most natural support is the combination of handlebars and saddle (if you wondered why saddles extend forward between riders' legs, this is one of the reasons). Standing up to pedal allows using more force on the pedals and loses the support from saddle at the same time. This requires more force handlebars, but also allows another option: Rocking the bike from side to side brings the pedal that is pushed downwards closer to the centerline, which reduces the torque that needs to be countered.

From mechanics standpoint, the arms don't do any work when holding the bike upright. Holding the bike upright by twisting the handlebars wouldn't indeed take any effort if you could just lock your arms like suspension lockout. Unfortunately muscles don't work that way, so maintaining the bike upright while pedaling standing takes more effort than rocking the bike around.

This method could be used to control balance when sitting in saddle, too. The problem is that when sitting, you need to wiggle your lower body to keep up with the saddle and the upper body in other direction to maintain balance. You can do it if you really want, but it is not comfortable or efficient.