Cities are perhaps the ultimate expression of human sociality displaying at once humanity’s greatest achievements and some of its most difficult challenges. Despite the increasing importance of cities in human societies our ability to understand them scientifically, and manage them in practice, has remained unsatisfactorily limited. The greatest difficulties to any scientific approach to cities have resulted from their many interdependent facets, as social, economic, infrastructural and spatial complex systems, which exist in similar but changing forms over a huge range of scales. Here, I show how cities may evolve following a small set of basic principles that operate locally and can explain how cities change gradually from the bottom-up. As a result I obtain a theoretical framework that derives the general open-ended properties of cities through the optimization of a set of local conditions. This framework is used to predict, in a unified and quantitative way, the average social, spatial and infrastructural properties of cities as a set of scaling relations that apply to all urban systems, many of which have been observed in nations around the world. Finally, I compare and contrast the structure and dynamics of cities to those of other complex systems that share some analogous properties.