Based on first-principles calculations, the evolution of the electronic and magnetic properties of transition metal dihalides MX$_2$ (M= V, Mn, Fe, Co, Ni; X = Cl, Br, I) is analyzed from the bulk to the monolayer limit. A variety of magnetic ground states is obtained as a result of the competition between direct exchange and superexchange. The results predict that FeX$_2$, NiX$_2$, CoCl$_2$ and CoBr$_2$ monolayers are ferromagnetic insulators with sizable magnetocrystalline anisotropies. This makes them ideal candidates for robust ferromagnetism at the single layer level. Our results also highlight the importance of spin-orbit coupling to obtain the correct ground state.