The sun heats the earth through radiation. Since there is no medium (like the gas in our atmosphere) in space, radiation is the primary way that heat travels in space. When theheat reaches the earth it warms the molecules of the atmosphere, and they warm other molecules and so on.
I hope this helps!