In light of increasing integration of renewable and distributed energy sources, power systems are undergoing significant changes. Due to the fast dynamics of such sources, the system is in many cases not quasi-static, and cannot be accurately described by time-varying phasors. In such systems the classic power flow equations do not apply, and alternative models should be used instead. In this light, this paper offers a tutorial on the dynamics and control of power systems with distributed and renewable energy sources, based mainly on the dq0 transformation. The paper opens by recalling basic concepts of dq0 quantities and dq0-based models. We then explain how to model and analyze passive networks, synchronous machines, three-phase inverters, and how to systematically construct dq0-based models of complex systems. We also highlight the idea that dq0 models may be viewed as a natural extension of time-varying phasor models, and discuss the correct use and validity of each approach.