It is true that air is a combination of nitrogen, oxygen, CO2 and other gases but each one is created in a different process. The earth's gravity keeps these gases from floating into outer space. The air becomes thiner and thiner as you rise in elevation till it basically becomes so thin that we consider anything beyond this line outer space. The equilibrium between these gases is kept thanks biological processes constantly happening in our world.
Hope this helped a bit.