How does photosynthesis work?
A leaf takes in water from the roots and carbon dioxide from the air, and uses the energy in sunlight to join them into sugar. Oxygen is left over and goes out into the air.

Why tho? Three levels deeper
Why are leaves green?
Chlorophyll, the molecule that catches the light, absorbs red and blue light strongly but reflects much of the green. The green light bounces back to your eyes.
Where does the oxygen come from?
From the water, not the carbon dioxide. Inside the leaf, light energy is used to split water molecules; their oxygen is released as oxygen gas. Experiments with labelled oxygen atoms showed this in the 1940s.
So where does a tree's wood come from?
Mostly from the air. The carbon in wood comes from the carbon dioxide the leaves took in, built into sugars and then into the tree's body. The soil mainly supplies water and small amounts of minerals.
The recipe
Water + carbon dioxide + light energy → sugar + oxygen. In chemistry terms: 6 CO₂ + 6 H₂O → C₆H₁₂O₆ + 6 O₂, powered by light.
Where it happens
Inside leaf cells are tiny green parts called chloroplasts, full of chlorophyll. They catch the light, and their machinery uses its energy to build sugar.
Why it matters to you
Almost every bite of food you eat traces back to photosynthesis, and so does most of the oxygen you breathe.
Check yourself
A plant sits in a sealed jar in the dark for days. What happens to the oxygen in the jar?
Answer
It goes down. With no light there's no photosynthesis, but the plant still breathes (respires), using oxygen to burn its sugars.