Plants obtain the gases they need through their leaves. They require oxygen for respiration and carbon dioxide for photosynthesis.
The gases diffuse into the intercellular spaces of the leaf through pores, which are normally on the underside of the leaf - stomata. From these spaces they will diffuse into the cells that require them.
Stomatal opening and closing depends on changes in the turgor of the guard cells. When water flows into the guard cells by osmosis, their turgor increases and they expand. Due to the relatively inelastic inner wall, the guard cells bend and draw away from each other, so the pore opens. If the guard cells loose water the opposite happens and the pore closes. The guard cells lower their water potential to draw in water from the surrounding epidermal cells, by actively accumulating potassium ions. This requires energy in the form of ATP which, is supplied by the chloroplasts in the guard cells.
Respiration occurs throughout the day and night, providing the plant with a supply of energy. Photosynthesis can only occur during sunlight hours so it stops at night. A product of respiration is carbon dioxide.
This can be used directly by the plant in photosynthesis.
However, during the day, photosynthesis can be going 10 or even 20 times faster than respiration (depending on light intensity), so the stomata must stay open so that the plant has enough carbon dioxide, most of which diffuses in from the external atmosphere.