Well, the particles gain energy from heat which makes the substance hotter.The more energy the particles gain, the hotter the substance.
When the substance is cooled the particles start to lose energy.The colder area that the substance is in, the more energy the particles lose.