Gas from Earth's core came to the surface through volcanoes.
Recent hypotheses about the origin of organic molecules suggest that these molecules may have formed in hydrothermal vents deep in the oceans, where hot gases and elements emerge from cracks in Earth's crust.
During the day, plants carry out photosynthesis releasing carbon dioxide. During the night, there isn't any sunlight so the plants carry out cellular respiration releasing oxygen.