I looked for some good websites that introduce the topic of gas exchange in the lungs, but there are a lot of websites to choose from. Below are a few that might be helpful. Basically, the higher the partial pressure of O2 in the air inhaled, the greater the amount of O2 dissolved in the blood and bound to hemoglobin will be. At higher altitude, the total pressure is lower, so the partial pressure of O2 is lower also. This results in less O2 in the blood.
Whether O2 or CO2 diffuses into the blood from the alveoli in the lungs or out of the blood into the alveoli depends on the difference in concentration, or the concentration gradient, of the two sides. If the concentration of O2, for example, is higher in the alveoli, then O2 will diffuse into the blood.
There is a lot to this subject, but that's the general idea!
Steve