We study the existence and regularity of optimal domains for functionals depending on the spectrum of the Dirichlet Laplacian or of more general Schrödinger operators. The domains are subject to perimeter and volume constraints; we also take into account the possible presence of geometric obstacles. We investigate the properties of the optimal sets and of the optimal state functions. In particular, we prove that the eigenfunctions are Lipschitz continuous up to the boundary and that the optimal sets subject to the perimeter constraint have regular free boundary. We also consider spectral optimization problems in non-Euclidean settings and optimization problems for potentials and measures, as well as multiphase and optimal partition problems.1. Introduction and examples.- 2. Shape optimization problems in a box.- 3. Capacitary measures.- 4. Subsolutions of shape functionals.- 5. Shape supersolutions and quasi-minimizers.- 6. Spectral optimization problems in R^d.- 7. Shape optimization problems for graphs.- Bibliography.