As you heat a magnet you supply it with more thermal energy, so the individual electron spins (like tiny magnets themselves) become more likely to be in high-energy states, pointing oppositely to their neighbors. That means that theyre less lined up so the total magnetism is reduced. At some point, in between the weakening of the overall magnetism and the availability of extra thermal energy, it becomes easy for domain walls- the boundaries between regions that are lined up pointing different directions- to slide around. Then the domains will rearrange so that they reduce the large-scale field energy by pointing different directions. That means that your permanent magnet is no longer overall magnetized. As you heat further, individual spins within domains become more likely to point opposite to their neighbors, and that reduces the average alignment seen by their neighbors too, reducing the effect which favors their having lined up in the first place. At a well-defined temperature, called the Curie temperature, the whole tendency to align into domains collapses, and the material ceases to be a ferromagnet at all. Cooling the material will cause magnetic domains to form again at the Curie temperature, but unless an external field is applied as the material cools, the domains will point all different directions, so you wont have a net magnetized permanent magnet.