I would recommend going to a text book for a rigorous treatment as Ger Koper suggests, but here is the more hand-waving understanding I have: Because of the Pauli exclusion principle, whether or not the spin of electrons match determines if they can hop between atoms (because they cannot occupy the same quantum states). Therefore if hopping is energetically unfavourable, electrons will want to align, because these wavefunctions don't involve hopping, this is a ferromagnet. If hopping is energetically favoured, then they will anti-align, creating an anti-ferromagnet. Because it involves exchange statistics, which have no classical analogue, this mechanism is fundamentally quantum. Of course the picture I gave here is quite simplified, and there are likely other important factors in real materials, but I hope this gives you an intuitive idea.