When the Earth was first formed, about 4.6 billion years ago, there was almost no oxygen in the atmosphere. Because the early Earth was very hot, there were a lot of volcanoes. Two of the things that come out of volcanoes are water (which formed the oceans) and carbon dioxide (CO2). The first organisms that developed on Earth used that carbon dioxide and produced oxygen. Over the course of billions of years, enough oxygen was in the atmosphere to do two major things. The first was that the oxygen could be changed into ozone, in order to help protect the Earth from ultraviolet rays (UV). UV rays can kill cells, so it was very important that there was a layer of ozone before plants, animals and fungi (all multicellular organisms) could develop.
The second major consequence of oxygen in the atmosphere was that animals and some fungi could then use that oxygen to breathe. Even more importantly, those animals and fungi give off carbon dioxide so that the amount of oxygen and carbon dioxide stays about the same.
Right now, global warming is happening because there is too much carbon dioxide in the atmosphere. This is because we are burning things (and burning requires oxygen) that have carbon in them, like gasoline. Just like the volcanoes, carbon dioxide is given off into the atmosphere when you burn anything with carbon.