Surrounding each of the lung's 300 to 400 million alveoli is a complex network of pulmonary capillaries. These tiny blood vessels carry blood low in oxygen from the right ventricle of the heart. The capillaries then carry the oxygen-rich blood to the left atrium.
During inspiration, the air that enters the alveoli has a high partial pressure of oxygen relative to the blood in the capillaries. Oxygen crosses the barrier of the alveoli and capillary walls—a distance of 0.5 μ—attempting to correct this disparity in a process called diffusion. During diffusion, molecules at a high concentration will cross a membrane in order to equalize the concentration of that molecule on the opposite side of the barrier. Within the capillaries, molecules called hemoglobin in the red blood cells bind the oxygen molecules, increasing the concentration and partial pressure of oxygen in the bloodstream.
During expiration, the air in the alveoli has a low partial pressure of carbon dioxide relative to the blood. Carbon dioxide also crosses the capillary wall/alveoli barrier through diffusion. This lowers the partial pressure and concentration of carbon dioxide in the blood that is on its way to the left atrium, and later onto the rest of the body.
It is a misconception that blood coming into the lungs doesn't have oxygen, and blood leaving the lungs doesn't have carbon dioxide. In actuality, the two molecules are ever-present in the bloodstream. The concentrations merely vary before and after a trip to the lungs.