Each municipality forms part of a province which in turn forms part or the whole of an autonomous community (17 in total plus Ceuta and Melilla): some autonomous communities have additional second level subdivisions, such as comarcas (districts) or mancomunidades (commonwealths). There are a total of 8,118 municipalities in Spain, including the autonomous cities of Ceuta and Melilla. In the Principality of Asturias, municipalities are officially named concejos (councils).
The average population of a municipality is about 5,300, but this figure masks a huge range: the most populous Spanish municipality is the city of Madrid, with a population of 3,141,991 (2015), while several rural municipalities have fewer than ten inhabitants (Villarroya, La Rioja, had a population of eight in 2015). The area of the municipal territory (Spanish: término municipal) usually ranges 2–40 km², but municipalities such as Tremp (Lleida) cover more than 300 km².
The organisation of the municipalities is governed by a 2 April 1985 law, completed by the 18 April 1986 royal decree. The Statutes of Autonomy of the various autonomous communities also contain provisions concerning the relations between the municipalities and the autonomous governments. In general, municipalities enjoy a large degree of autonomy in their local affairs: many of the functions of the comarcas and provinces are municipal powers pooled together.
Each municipality is a corporation with independent legal personality: its governing body is called the ayuntamiento (municipal council or corporation), a term often also used to refer to the municipal offices (city and town halls). The ayuntamiento is composed of the mayor (Spanish: alcalde), the deputy mayors (Spanish: tenientes de alcalde) and the plenary assembly (pleno) of councillors (concejales).
The mayor and the deputy mayors are elected by the plenary assembly, which is itself elected by universal suffrage on a list system every four years. The plenary assembly must meet publicly at least every three months at the seat of the ayuntamiento. Many ayuntamientos also have a governing commission (comisión de gobierno), named by the mayor from among the councillors; it is required for municipalities of more than 5,000 inhabitants. The governing commission, whose role is to assist the mayor between meetings of the plenary assembly, may not include more than one third of the councillors.