Beer is carbonated under pressure, and the moment a can or bottle is opened that pressure equalises with the room. Carbon dioxide begins leaving the liquid immediately, and unlike wine, there is no practical way to reverse it.
What is actually happening
Carbon dioxide dissolves in beer under pressure and stays there while the container is sealed. Opening it releases the pressure, and the gas comes out of solution and escapes into the air.
Anything that increases surface area or agitation accelerates it: pouring, swirling, warming and stirring all drive gas out faster.
Temperature is the largest factor. Gas is far more soluble in cold liquid, so a warm beer loses carbonation quickly while a cold one holds it. This is why a can left out on a worktop goes flat noticeably faster than one in the fridge.
Why it cannot be put back
Recarbonating requires pressure, and a domestic setting does not provide it. Carbonation devices designed for water can technically force gas into beer, and the results are generally poor: the beer foams uncontrollably and the carbonation feels harsh rather than integrated.
Sealing a partly drunk can with a stopper slows the loss and does not restore what has gone.
Bottle-conditioned beers are a partial exception, since live yeast continues producing a small amount of gas, but not at a rate that rescues an opened bottle.
Slowing the loss
If a beer must be kept for a few hours, three things help.
Refrigerate it, since cold holds gas in solution far better than warmth.
Seal it properly. A purpose-made can cap or a bottle stopper with a rubber seal is far better than a loosely replaced crown cap, and the sparkling-wine stoppers with hinged arms work well on bottles.
Reduce the headspace. Decanting the remainder into a smaller bottle filled to the neck slows both the carbonation loss and the oxidation.
Even with all three, a few hours is realistic and overnight is optimistic.
The other half of the problem
Flatness rarely arrives alone. An opened beer is also oxidising, and the stale flavours that follow, wet cardboard and a dull, papery finish, appear over the same period.
So a beer left open overnight is not simply less fizzy; it tastes worse in ways carbonation would not fix.
When a fresh beer arrives flat
If a sealed beer pours with no carbonation at all, something else is wrong.
A faulty seal on the can or crown cap is the usual cause, and the beer will taste stale as well as flat. A glass with detergent residue destroys the head while the beer itself remains carbonated, which is a different problem with a similar appearance.
On draught, a flat pint indicates a dispense fault, generally gas pressure set too low or a problem in the line, and it is worth mentioning at the bar.
What to do with it
Flat beer is not unsafe and is perfectly usable in the kitchen. It works in batter, in a beef stew, in bread, in a chilli or as a braising liquid, and the lost carbonation is irrelevant once it is cooked.
Freezing it in an ice cube tray gives portions for later, which is a better outcome than pouring it away.
The practical prevention is simpler than any of the remedies: open beer you intend to finish, and buy smaller cans or share larger ones rather than leaving half a bottle for tomorrow.
Deliberately flat beer
A few beers are meant to be still or nearly so. Unblended lambic is traditionally served with no carbonation at all, and some cask ales are naturally very lightly conditioned.
In those cases flatness is the style rather than a fault, and expecting a prickle from them is a misunderstanding.
The general rule is that a beer should have the carbonation its style calls for, which ranges from almost none to the vigorous fizz of a Belgian tripel, and judging every beer against a single expectation will mislead you in both directions.