Exploring the origins and evolution of magnetic fields in planets, stars and galaxies, this book gives a basic introduction to magnetohydrodynamics and surveys the observational data, with particular focus on geomagnetism and solar magnetism. Pioneering laboratory experiments that seek to replicate particular aspects of fluid dynamo action are also described. The authors provide a complete treatment of laminar dynamo theory, and of the mean-field electrodynamics that incorporates the effects of random waves and turbulence. Both dynamo theory and its counterpart, the theory of magnetic relaxation, are covered. Topological constraints associated with conservation of magnetic helicity are thoroughly explored and major challenges are addressed in areas such as fast-dynamo theory, accretion-disc dynamo theory and the theory of magnetostrophic turbulence. The book is aimed at graduate-level students in mathematics, physics, Earth sciences and astrophysics, and will be a valuable resource for researchers at all levels.