Shortly after the Earth was formed, the atmosphere contained very little oxygen. Around 2 billion years ago, the proportion of oxygen in the atmosphere increased to about 5%. The most likely cause of this change, though perhaps not the only one, was the evolution of photosynthesis. At some stage, probably around 2 billion years ago, bacteria in the oceans evolved the trick of using the energy of sunlight to turn water and carbon dioxide into sugar and oxygen. Plants use the same trick today, and they use the same molecules as one of the early bacteria did: chlorophyll. Animals proceed in pretty much the opposite direction: they power themselves by using oxygen to burn food, producing carbon dioxide instead of using it up. Those early photosynthesizing bacteria used the sugar for energy, and multiplied rapidly, but to them the oxygen was just a form of toxic waste, which bubbled up into the atmosphere. The oxygen level then stayed roughly constant until about 600 million years ago, when it underwent a rapid increase to the current level of 21%.
The amount of oxygen in today's atmosphere is far greater than could ever be sustained without the influence of living creatures, which not only produce oxygen in huge quantities but use it up again, in particular locking it up in carbon dioxide. It is startling how far 'out of balance' the atmosphere is, compared to what would happen if life were suddenly removed and only inorganic chemical processes could act. The amount of oxygen in the atmosphere is dynamic, it can change on a timescale that by geological standards is extremely rapid, a matter of centuries rather than millions of years. For example, if some disaster occurred which killed off all the plants but left all the animals, then the proportion of oxygen would halve in about 500 years, to the level on mountain peaks in the Andes today. The same goes for the scenario of 'nuclear winter' introduced by Carl Sagan, in which clouds of dust thrown into the atmosphere by a nuclear war stop most of the sunlight from reaching the ground. In this case, plants may still eke out some kind of existence, but they don't photosynthesize: they do use oxygen, though, and so do the microorganisms that break down dead plants.
The same screening effect could also occur if there were unusual numbers of active volcanoes, or if a big meteorite or comet hit the Earth. When comet Shoemaker-Levy 9 hit Jupiter in 1994, the impact was equivalent to half a million hydrogen bombs.
The 'budget' of income and expenditure for oxygen, and the associated but distinct budget for carbon, is still not understood. This is an enormously important question because it is vital background to the debate about global warming. Human activities, such as electrical power plants, industry, use of cars, or simply going about one's usual business and breathing while one does so, generate carbon dioxide. Carbon dioxide is a 'greenhouse gas' which traps incoming sunlight like the glass of a greenhouse. So if we produce too much carbon dioxide, the planet should warm up. This would have undesirable consequences, ranging from floods in low-lying regions such as Bangladesh to big changes in the geographical ranges of insects, which could inflict serious damage on crops. The question is: do these human activities actually increase the Earth's carbon dioxide, or does the planet compensate in some way? The answer makes the difference between imposing major restrictions on how people in developed (and developing) countries live their lives, and letting them continue along their current paths. The current consensus is that there are clear, though subtle, signs that human activities do increase the carbon dioxide levels, which is why major international treaties have been signed to reduce carbon dioxide output. (Actually taking that action, rather than just promising to do so, may prove to be a different matter altogether.)
The difficulties involved in being sure are many. We don't have good records of past levels of carbon dioxide, so we lack a suitable 'benchmark' against which to assess today's levels, although we're beginning to get a clearer picture thanks to ice cores drilled up from the Arctic and Antarctic, which contain trapped samples of ancient atmospheres. If 'global warming' is under way, it need not show up as an increase in temperature anyway (so the name is a bit silly). What it shows up as is climatic disturbance. So even though the six warmest summers in Britain this century have all occurred in the nineties, we can't simply conclude that 'it's getting warmer', and hence that global warming is a fact. The global climate varies wildly anyway, what would it be doing if we weren't here?
A project known as Biosphere II attempted to sort out the basic science of oxygen/carbon transactions in the global ecosystem by setting up a 'closed' ecology, a system with no inputs, beyond sunlight, and no outputs whatsoever. In form it was like a gigantic futuristic garden centre, with plants, insects, birds, mammals, and people living inside it. The idea was to keep the ecology working by choosing a design in which everything was recycled.
The project quickly ran into trouble: in order to keep it running, it was necessary to keep adding oxygen. The investigators therefore assumed that somehow oxygen was being lost. This turned out to be true, in a way, but for nowhere near as literal a reason. Even though the whole idea was to monitor chemical and other changes in a closed system, the investigators hadn't weighed how much carbon they'd introduced at the start. There were good reasons for the omission, mostly, it's extremely difficult, since you have to estimate carbon content from the wet weight of live plants. Not knowing how much carbon was really there to begin with, they couldn't keep track of what was happening to carbon monoxide and carbon dioxide. However, 'missing' oxygen ought to show up as increased carbon dioxide, and they could monitor the carbon dioxide level and see that it wasn't going up.
Eventually it turned out that the 'missing' oxygen wasn't escaping from the building: it was being turned into carbon dioxide. So why didn't they see increased carbon dioxide levels? Because, unknown to anybody, carbon dioxide was being absorbed by the building's concrete as it 'cured'. Every architect knows that this process goes on for ten years or so after concrete has set, but this knowledge is irrelevant to architecture. The experimental ecologists knew nothing about it at all, because esoteric properties of poured concrete don't normally feature in ecology courses, but to them the knowledge was vital.
Behind the unwarranted assumptions that were made about Biosphere II was a plausible but irrational belief that because carbon dioxide uses up oxygen when it is formed, then carbon dioxide is opposite to oxygen. That is, oxygen counts as a credit in the oxygen budget, but carbon dioxide counts as a debit. So when carbon dioxide disappears from the books, it is interpreted as a debt cancelled, that is, a credit. Actually, however, carbon dioxide contains a positive quantity of oxygen, so when you lose carbon dioxide you lose oxygen too. But since what you're looking for is an increase in carbon dioxide, you won't notice if some of it is being lost.
The fallacy of this kind of reasoning has far wider importance than the fate of Biosphere II. An important example within the general frame of the carbon/oxygen budget is the role of rainforests. In Brazil, the rainforests of the Amazon are being destroyed at an alarming rate by bulldozing and burning. There are many excellent reasons to prevent this continuing, loss of habitat for organisms, production of carbon dioxide from burning trees, destruction of the culture of native Indian tribes, and so on. What is not a good reason, though, is the phrase that is almost inevitably trotted out, to the effect that the rainforests are the 'lungs of the planet'. The image here is that the 'civilized' regions, that is, the industrialized ones, are net producers of carbon dioxide. The pristine rainforest, in contrast, produces a gentle but enormous oxygen breeze, while absorbing the excess carbon dioxide produced by all those nasty people with cars. It must do, surely? A forest is full of plants, and plants produce oxygen.