The lowering of temperature as you reach higher altitudes is due to the change in atmospheric pressure. You may be aware that the air around us is constantly exerting pressure on us due to there being lots of air above us weighing down on us. It sounds a bit strange to say that air weighs something but it does, we just don’t feel it because it is what we’re used to, just like gravity is constantly pulling us down.
There is a direct relationship between temperature and pressure. If you increase the pressure of a system then the temperature will get higher. This is why bicycle pumps can get hot after use. Decrease the pressure and the temperature goes down as can be experienced letting the air out of a balloon very quickly, the balloon gets cold.
As you go higher up, the pressure gets less and less due to there being less air above you weighing down on you, therefore the temperature goes down too.