Exact statistical mechanics of the Ising model on networks