While fermentation can take place under anaerobic (lack of oxygen) conditions, it can happen when oxygen is abundant too. Yeast, for instance, prefers fermentation to cellular respiration if enough glucose is available to support the process, even if plenty of oxygen is available.
When energy-rich sugar--glucose in particular--enters a cell, it is broken down in a process called glycolysis. Glycolysis is a prerequisite step both for cellular respiration and fermentation. It is a common pathway for the breakdown of sugar, which can lead to either process.
Glycolysis is an ancient biochemical process, having emerged very early in evolutionary history. The core reactions for glycolysis were "invented" by microorganisms long before photosynthesis, which emerged roughly 3.5 billion years ago, but which would take roughly 1.5 billion years to fill the seas and atmosphere with any appreciable amount of oxygen. Thus, even complex eukaryotes (the biological "domain" that includes the animal, plants, fungi and protist kingdoms) are capable of producing energy without oxygen. In yeast, which belong to the fungi kingdom, the chemical products of glycolysis are fermented to produce energy for the cell.
At the end of glycolysis, the six-carbon structure of glucose is split into two molecules of the three-carbon compound "pyruvate." Also produced is the chemical NADH, from a more "oxidized" chemical called NAD+. In yeast, pyruvate undergoes "reduction," the gaining of electrons, which are then transferred from the NADH produced earlier in glycolysis to yield acetaldehyde and carbon dioxide. Acetaldehyde then is reduced further to ethyl alcohol, the ultimate product of fermentation. In animals, including humans, pyruvate can be fermented when the availability of oxygen is low. This is especially true in muscle cells. When this happens, though tiny amounts of alcohol are produced, most of the pyruvate from glycolysis is reduced not to alcohol, but rather to lactic acid. While lactic acid can leave animal cells and be used to produce energy in the heart, it can build up within muscles, causing pain and decreased athletic performance.
The universal energy carrier in cells is a chemical known as ATP. If utilizing oxygen, cells can produce ATP through glycolysis followed by cellular respiration--such that one molecule of glucose sugar yields 36-38 molecules of ATP, depending on the cell type. Out of these 36-38 molecules of ATP, only two are produced during the glycolysis phase. Thus, if using fermentation as an alternative to cellular respiration, cells make a great deal less energy than they do using respiration.