Dynamical systems applied to cosmology: Dark energy and modified gravity