Two complementary effects modify the GHz magnetization dynamics of nanoscale heterostructures of ferromagnetic and normal materials relative to those of the isolated magnetic constituents: On the one hand, a time-dependent ferromagnetic magnetization pumps a spin angular-momentum flow into adjacent materials and, on the other hand, spin angular momentum is transferred between ferromagnets by an applied bias, causing mutual torques on the magnetizations. These phenomena are manifestly nonlocal: they are governed by the entire spin-coherent region that is limited in size by spin-flip relaxation processes. We review recent progress in understanding the magnetization dynamics in ferromagnetic heterostructures from first principles, focusing on the role of spin pumping in layered structures. The main body of the theory is semiclassical and based on a mean-field Stoner or spin-density--functional picture, but quantum-size effects and the role of electron-electron correlations are also discussed. A growing number of experiments support the theoretical predictions. The formalism should be useful to understand the physics and to engineer the characteristics of small devices such as magnetic random-access memory elements.