The trick, as you already suspected, is to chill the bottles well and avoid any agitation for at least several hours before opening. But knowing why always helps.
Beer, soda, and champagne all get their fizziness from carbon dioxide gas, which has been dissolved in the liquid during, or in the case of true champagne, after, the bottling process. The fizz consists of bubbles of carbon dioxide coming out of the liquid into the air. When that happens gently on our tongues, we get that nice tingly sensation. But when it happens too fast, we get the mop.
The amount of carbon dioxide that can remain peacefully dissolved in a liquid depends directly on how much carbon dioxide there is in the space above the liquid’s surface, because the more carbon dioxide molecules there are bouncing around in that space, the more of them will hit the surface and dissolve.
In the sealed bottle, that space is filled with carbon dioxide and air; furthermore, these gases are packed in very tightly, at a pressure that can be as high as sixty pounds per square inch or 4.2 kilograms per square centimeter. (The air pressure in your auto tires is only about half that much.) So there is lots of carbon dioxide dissolved in the liquid when it comes from the bottler.
When the bottle is opened, no matter how gently, the pressurized carbon dioxide escapes and only normal air at normal pressure exists above the surface. In normal air, only about one out of every three thousand molecules is carbon dioxide. So practically all of the dissolved carbon dioxide has to come out of the liquid in one way or another. The only question is, how fast? And the answer is, it’s normally quite a slow process.
After an initial burst of escapees from the air space, the dissolved carbon dioxide molecules don’t leave the liquid all at once. If they did, your beverage would go flat as a shadow in an instant, making one hell of an explosion no matter how gently you had handled the bottle.
Neither can the gas molecules leave one at a time from deep within the liquid. They have to find some rallying points, some unique meeting places at which they can congregate and form groups, bubbles, that are big enough to muscle their way up and out of the liquid. Scientists refer to these congregation sites as nuclei (plural of nucleus).
Almost any break in the homogeneity of the liquid, even a microscopic speck of dust, will serve as a nucleus for the formation of bubbles. So will tiny scratches on the surface of the glass, because they can trap microscopic air bubbles when the beverage is poured, and these air bubbles will invite more gas molecules to join them. Carbon dioxide molecules congregate at all of these nuclei and grow into bubbles, which rise as soon as they are big and buoyant enough to push their way upward through the liquid.
What does all this have to do with shaking the bottle? Well, when you shake the bottle, you are trapping some of the gas that was above the liquid, making tiny bubbles out of it. And these tiny bubbles are the best possible nuclei for the further growth of bubbles. The carbon dioxide molecules in the liquid latch onto these new bubbles, which grow into bigger and bigger bubbles. Before you know it you have a foaming mess, propelled out of the neck of the bottle by expanding gas pressure like a pellet out of an air rifle.
You will run into the same problem, but probably not as bad, if you open beer, soda, or champagne that isn’t chilled enough. Carbon dioxide is less soluble in warmer liquids so more gas will rush out than if the liquid were cold. If you have also shaken the bottle a lot, well, that’s just too awful to contemplate.