The exchange interaction comes up from the superposition of the electron wave functions in the crystal. These wave functions are solution of the Schrödinger's equation. For simplicity we can consider only the interaction betweens ions and electrons, i.e. just Coulomb interaction.
Roughly, according to Pauli principle if the spatial wave functions are odd, the spins align in the same direction, so the material is ferromagnetic.
If the spatial wave function are even, the spins align in opposite direction, so the material is anti-ferromagnetic.
You can find the demonstration of the exchange interaction in this book: Introduction to the Theory of Ferromagnetism, Amikam Aharoni - Clarendon Press - Oxford 1998.