Every new car, and most cars produced after 1980, have an oxygen sensor. The sensor is part of the emissions control system and feeds data to the engine management computer. The goal of the sensor is to help the engine run as efficiently as possible and also to produce as few emissions as possible.
A gasoline engine burns gasoline in the presence of oxygen (see How Car Engines Work for complete details). It turns out that there is a particular ratio of air and gasoline that is “perfect,” and that ratio is 14.7:1 (different fuels have different perfect ratios — the ratio depends on the amount of hydrogen and carbon found in a given amount of fuel). If there is less air than this perfect ratio, then there will be fuel left over after combustion. This is called a rich mixture. Rich mixtures are bad because the unburned fuel creates pollution. If there is more air than this perfect ratio, then there is excess oxygen. This is called a lean mixture. A lean mixture tends to produce more nitrogen-oxide pollutants, and, in some cases, it can cause poor performance and even engine damage.
The oxygen sensor is positioned in the exhaust pipe and can detect rich and lean mixtures. The mechanism in most sensors involves a chemical reaction that generates a voltage (see the patents below for details). The engine’s computer looks at the voltage to determine if the mixture is rich or lean, and adjusts the amount of fuel entering the engine accordingly.
The reason why the engine needs the oxygen sensor is because the amount of oxygen that the engine can pull in depends on all sorts of things, such as the altitude, the temperature of the air, the temperature of the engine, the barometric pressure, the load on the engine, etc.
When the oxygen sensor fails, the computer can no longer sense the air/fuel ratio, so it ends up guessing. Your car performs poorly and uses more fuel than it needs to.