Zipf's law is one of the few quantitative reproducible regularities found in economics. It states that, for most countries, the size distributions of cities and of firms are power laws with a specific exponent: the number of cities and of firms with sizes greater than S is inversely proportional to S. Zipf's law also holds in many other scientific fields. Most explanations start with Gibrat's law of proportional growth (also known as 'preferential attachment'' in the application to network growth) but need to incorporate additional constraints and ingredients introducing deviations from it. This book presents a general theoretical derivation of Zipf's law, providing a synthesis and extension of previous approaches. The general theory is presented in the language of firm dynamics for the sake of convenience but applies to many other systems. It takes into account (i) time-varying firm creation, (ii) firm's exit resulting from both a lack of sufficient capital and sudden external shocks, (iii) the coupling between firm's birth rate and the growth of the value of the population of firms. The robustness of Zipf's law is understood from the approximate validity of a general balance condition. A classification of the mechanisms responsible for deviations from Zipf's law is also offered.