In ergodic physical systems, time-averaged quantities converge (for large times) to their ensembleaveraged values. Large deviation theory describes rare events where these time averages differ significantly from the corresponding ensemble averages. It allows estimation of the probabilities of these events, and their mechanisms. This theory has been applied to a range of physical systems, where it has yielded new insights into entropy production, current fluctuations, metastability, transport processes, and glassy behaviour. We review some of these developments, identifying general principles. We discuss a selection of dynamical phase transitions, and we highlight some connections between large-deviation theory and optimal control theory. arXiv:1910.09883v1 [cond-mat.stat-mech]