How the alveoli works:
On inhaling, oxygen accumulates in high concentration inside the alveoli. Because the capillaries which pass the alveoli are low in oxygen (because the blood has just returned from the body tissues), oxygen passes across the thin walls of the alveoli directly into the blood, and hence into the erythrocytes (red blood cells).
At the same time, the carbon dioxide molecules are in greater concentration in the blood than they are in the alveoli. Carbon dioxide diffuses directly from the capillaries across their thin walls into the air space of the alveoli from whence it may be expelled upon exhaling. These combined actions increase the oxygen concentration and decreases the concentration of carbon dioxide in the blood as it leaves the alveolus. This process of bringing fresh (oxygenated) air to the alveoli and removing the air high in carbon dioxide is known as ventilation.