FRom the Wikipedia page 
The President of the Community of Madrid is the highest-ranking officer of the Autonomous Community of Madrid and the head of the Executive Branch.
In the process of the democracy restoration in Spain between 1975–1978, the nationalist and regionalist parties pressed to grant home rule to parts of Spain. Finally, the Constitution stated that any province or group of provinces could form an autonomous community and thus be granted partial home rule. The Autonomous Community of Madrid (Spanish Comunidad Autónoma de Madrid) was created in 1982, and since then regional elections are held every 4 years.
The citizens of the Autonomous Communities of Spain do not elect a person for presidency of their community: but rather they elect the regional legislature, and that legislature elects the regional President. This system usually assures the government more stability because a candidate needs a majority (that is supposed to be loyal to him/her during the whole term) to be elected, but the top-voted party can be denied the right to form the government. This situation, though infrequent in nationwide elections, often happens in local/regional legislatures throughout Spain: the most usual coalition is between the Socialist Party (PSOE) and the United Left (IU).
In Madrid, such a coalition was formed in the 2nd term, in which the incumbent Socialist Joaquín Leguina obtained the most vote but not a majority, once more in the 3rd term, allowing him to remain in office even after having obtained fewer votes than the People's Party (PP), and once more in the 6th term, by the PSOE candidate Rafael Simancas. However, this last coalition ultimately failed due to the dissidence of two PSOE Assembly Members, which denounced the pact with IU as being too wide and unrepresentative of the people's will due to the planned power balance. Elections were repeated after a few weeks and Partido Popular won, then by absolute majority.
Since then, the People's Party victories by absolute majority were repeated in 2007 and 2011's regional elections. After the resignation of President Esperanza Aguirre, nominally due to personal matters in late 2012, her deputy president Ignacio González González held the post. The People's Party obtained the most votes in 2015 but lost its absolute majority. Its candidate Cristina Cifuentes was invested President after an agreement with Citizens. She was replaced by her deputy Ángel Garrido when she resigned after evidence of both a fake Master's degree and petty shoplifting came out.