Gaseous oxygen was created on the Earth about 3.5 billion years ago. At that time, the UV light of the sun decomposed the water vapour molecules present in the atmosphere, and released oxygen and hydrogen.
But a major part of the oxygen immediately reacted with other substances, so that it was no longer available in the atmosphere as gas. Oxygen was also produced by the seas, where the 'blue algae' carried out photosynthesis.
These bacteria converted sunlight and carbon dioxide into energy. In the process, gaseous oxygen was released as a 'waste product', which accumulated in the atmosphere. About one billion years ago, about one-twentieth (4%) of the atmosphere consisted of oxygen.