Let's work this question with a single alveoli. Each alveoli is wrapped in a "net" of capillaries. Think of it like a soccer ball in a mesh bag. The ball is the alveoli. The mesh is the capillaries.
Blood is pumped from the right side of the heart to the lungs and alveoli. Blood from the right side of the heart is low in oxygen content and high in carbon dioxide content. It has been sent to the lungs to unload carbon dioxide and take on oxygen.
When you inhale, the alveoli are filled with oxygen. This means that the alveoli contain a higher concentration of oxygen than the surrounding capillaries. Due to diffusion, gasses move from high concentrations to lower concentrations; therefore, oxygen diffuses out of the alveoli and into the capillaries. The opposite happens with carbon dioxide. The blood being sent to the lungs is high in carbon dioxide content, and the freshly inhaled breath is low in carbon dioxide content; therefore carbon dioxide diffuses from the blood and into the alveoli. It is then exhaled. This process occurs across all 600 million alveoli every time that you inhale and exhale.