The atmosphere on early Earth was very different than what we have today. It is thought that the first atmosphere of the Earth was made up of hydrogen and helium, much like the gaseous planets and the sun. After millions of years of volcanic eruptions and other internal Earth processes, the second atmosphere emerged. This atmosphere was full of greenhouse gases like carbon dioxide, sulfur dioxide, and also contained other types of vapors and gases like water vapor and to a lesser extent ammonia and methane.
This combination of gases was very inhospitable to most forms of life. While there are many theories, such as the Primordial Soup Theory, Hydrothermal Vent Theory, and the Panspermia Theory of how life began on Earth, it is certain that the first organisms to inhabit the Earth had to not need oxygen, as there was no free oxygen in the atmosphere. Most scientists agree that the building blocks of life would not have been able to form if there had been oxygen in the atmosphere at that time.
However, plants and other autotrophic organisms would thrive in an atmosphere filled with carbon dioxide. Carbon dioxide is one of the main reactants necessary for photosynthesis to occur. With carbon dioxide and water, an autotroph can produce a carbohydrate for energy and oxygen as waste. After many plants evolved on Earth, there was an abundance of oxygen floating freely in the atmosphere. This was toxic to some autotrophs and they became extinct.
Oxygen wasn't all bad for the living organisms of that time. Oxygen gas floated to the top of the atmosphere where it was exposed to ultraviolet rays of the sun. Those UV rays split the diatomic oxygen molecules and helped to create ozone, which is made up of three oxygen atoms covalently bonded to one another. The ozone layer helped block some of the UV rays from reaching Earth. This made it safer for life to colonize on land without being susceptible to those damaging rays.
With a protective layer of ozone to cover them and plenty of oxygen gas to breathe, heterotrophs were able to evolve. The first consumers to appear were simple herbivores that could eat the plants that survived the oxygen laden atmosphere. More heterotrophs could then evolve as there were more food sources. These heterotrophs happened to release carbon dioxide as a waste product of their cellular respiration. The give and take of the autotrophs and heterotrophs were able to keep levels of oxygen and carbon dioxide in the atmosphere steady.