Hierarchical structures are ubiquitous in human and animal societies, but a fundamental understanding of their raison d’être has been lacking. Here, we present a general theory in which hierarchies are obtained as the optimal design that strikes a balance between the benefits of group productivity and the costs of communication for coordination. By maximising a generic representation of the output of a hierarchical organization with respect to its design, the optimal configuration of group sizes at different levels can be determined. With very few ingredients, a wide variety of hierarchically ordered complex organisational structures can be derived. Furthermore, our results rationalise the ubiquitous occurrence of triadic hierarchies, i.e.,...