What explains why matter expands when heated and contracts when cooled?

The kinetic molecular theory. Added heat in a solid causes more vibration of the molecules, which move them apart.