Your centre of gravity (CoG) aka centre of mass (of you + bike) is balancing over the contact patches of the tires. In fact, over the line connecting the two patches.
Standing up raises your CoG higher up which helps some (try balancing a broom on your palm head-end up vs. handle-end up). A longer pendulum tips more slowly.
But much more importantly, having your butt off the seat lets you shift your weight sideways much further and faster. You're pushing sideways on the pedals (and some handlebars but mostly pedals) to do this.
This applies a sideways force to the ground via your tires, and an equal and opposite net force on your centre of gravity. So you can apply a centering horizontal force on your own CoG, letting you get it back over the balance point on the ground.
It also lets you tip the bike, moving some close-to-the-ground mass one way, and your torso the other.
If you start to tip left, you tip the bike more left, but move your body right. The centre of mass of the whole you+bike system moves to the right, because you were able to push sideways on the ground to make that happen.
If all you could do is lean your shoulders (because your butt is stuck to the seat), that's not helpful because you're moving a high-up part of your body.
The lever arm from pedals to ground is short, far below your CoG. I think this is relevant to being able to generate horizontal force on the ground. Tipping the bike left/right levers you sideways, like moving your palm under a broom handle that you're balancing. (I think).