First of all you need to understand that molecules have motion. All molecules higher than the temperature of absolute zero (0 degrees Kelvin) move in proportion to their temperature. In other words, hot molecules will have a higher degree of motion than cold molecules. When a solid is heated up, its molecules will absorb that energy and increase their molecular motion. So as the material goes from a solid to a liquid, the molecular motion increases as does the space between the molecules. This increased space and motion is what allows a liquid to be less dense than a solid (in general) and allows it to move spontaneously to fill the space of its container. Continued heating will allow the liquid to absorb even more energy and boil to become a gas, which is even less dense with more space between molecules and a higher degree of molecular motion than either a liquid or a solid.