The Earth formed around 4.6 BYA. Since then, the gaseous envelope, the atmosphere has changed over billions of years.
Around 4 BYA, the atmosphere's composition was large amounts of poisonous methane and ammonia gases and water vapor. However at 3.8 BYA the process of nuclear fission using the sun's ultraviolet rays or solar energy broke down these poisonous gases and other gases such as Nitrogen, Hydrogen, Carbon Dioxide formed. 200 million years later Oxygen another gas essential to life formed through this chemical reaction. The water vapor remained.
With the help the Cyanobacteria, which is a microorganism and the only sign of life back then and it was anaerobic / didn't need air to live, more oxygen was produced into the atmosphere.
At 3.5 BYA, hydrogen was escaping into space because earth's gravity, the force of attraction, wasn't strong enough. Oxygen went through more chemical reactions caused by the solar energy which produced O3 or Ozone, a form of oxygen with very powerful oxidizing properties. Overtime as more ozone was produced, a layer formed about 30km above the surface in the stratosphere called the Ozone Layer.
This ozone layer protects life on earth from harmful ultraviolet rays which can cause ultraviolet radiation or serious cellular damage.
Eventually around 700 MYA, due to the protection of from the ozone layer, microorganisms rose to the top of the oceans. Using the solar energy and combing CO2 and water in the air, the microorganisms produced food for themselves and more oxygen as a byproduct, this is the process of photosynthesis. Later on, some of these microorganism headed out onto land.
600 MYA the levels of Carbon Dioxide and Oxygen started to level off to what the percentages are present day. Today the composition is 79% Nitrogen, 20% oxygen, and 1% other. The high amounts of nitrogen are due to the decaying of plants and animals and volcanic activity and other natural processes.