How did oxygen come to make up up 21 percent of earth's atmosphere?

Through the process of photosynthesis algae and later plants used the energy of sunlight to convert water and carbon dioxide into sugar and oxygen. Oxygen levels rose until they reached a high point of 36% about 300 million years ago. Climate shifts and changes in life on Earth later caused some of that oxygen to go back into carbon dioxide, which was sequestered in carbonate rocks, and oxygen levels began to fluctuate. Currently, oxygen levels are at about 21% but that may change over the next few million years.