Well, I know that there are different states of matter like gas, solid, and liquid
so how do they gain or lose energy?
By changing the tempature
yea, if the temperature is right
See it rains, but if it turns to cold it will freeze into either snow or ice/hail
Or if it gets to hot it starts to evaporate right?
See the water froze because the temperature got to low to stay in its regular form because the molecules get packed together.
Yea. It depends on the energy levels in the matter. For example gas has the highest energy level the particles move super fast and theres only a few atoms inside.