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., of the universal preferred scaling ratio between 3 and 4 found in many human and animal hierarchies, which should occur according to our theory when production is rather evenly contributed by all levels. We also provide a systematic approach for optimising team organisation, helping to address the question of the optimal ‘span of control’. The significantly larger number ∼ 3 − 20 of subordinates a supervisor typically manages is rationalised to occur in organisations where the production is essentially done at the bottom level and in which the higher levels are only present to optimise coordination and control.