Ok, I will be brief.
Before the historical part, the general notes:
Christianism in general and Catholicism love hierarchies: God -> Saints -> Men; Pope -> Bishops -> Priests -> Population, all of which was used for political justifications King -> Noble -> Serf, King/President -> Capitalist/Landowner -> Worker.
Traditionally, the Church was one of the occupations of choice for the minor children of the nobility; and those would go directly to occupy the top positions of the Church. The alliance Church/Ancient Regime was not only ideological but also familiar.
Certainly, bishops and other ecclesiastical dignities had a rather comfortable living, on account of the Church wealth.
Rich and powerful people were the people who could donate great wealth to the Church, increasing their influence.
To the historical Spanish part:
First, the Catholic Church (or FWIW any religion) has influence in its believers: a King going against the Church could be excomunicated, which would give all of his enemies a legal and moral reason for rebellion. Also, for a long time the Catholic Church was the closer equivalent to a "civil service" available. They had direct contact with the population, registered people, collected taxes both for themselves and the Crown1.
As such, Kings and Church established a quid pro quo, in exchange of the Church support the Kings gave the Church privileges and made heretics enemy of the state2.
Now come the Illustration and religion, the Church and its power start being openly criticized by part of the intellectual class, that for once have the favor of the Kings. Not all the criticism was antireligious or anticlerical3, but the answer of the Church was to try to keep its privileges and influence. All of that is compounded by the decline of the Spanish Empire (certified by the War of Spanish Sucesion) and the evidence that the country was becoming underdeveloped in relation with other European countries.
Come the French invasion of Spain, and the country divides in three:
Those who thought that, while invaders, the French could be useful to finish the obscurantism that mired the Spanish culture and advance the country.
Those who fought against the French but were in line with the more moderate ideas of the French Revolution and -mainly- English Parliamentarism, the liberals.
Those who fought against the French mainly because of the ideas from the French Revolution that they brought with them4. MANY priests among those.
The French are defeated, Ferdinand VII returns. Starts swinging towards absolutism and Old Regime (including Church privileges) again, a coup in 1820 forces him to agree with the liberals, the intervention of 1823 of the Holy Alliance allows him to suppress the liberals but nonetheless in a more moderate way.
During the liberal period some properties of the Church are confiscated as a way to get funds for the country, as it was currently fighting against the independence movements in South America and a pro-absolutist rebellion. When absolutism is reinstated, those properties are returned to the Church.
At his deathbed, he swings his support as heir between his daughter Isabella and his brother Carlos. The Traditionalist do not want anything to do with a woman, so they flock to Carlos, and Isabella gathers the support of the liberals5
The Carlist Wars follow, and again the Traditionalists identify themselves as the "religious" side, promising to restore the privileges and properties of the Church. They get support from the more radical parts of the Church6. By their side, one of the ways that the liberals use to fund the war is, again, to confiscate part of the Church property7. Another of their points is the creation of government structures based in the civil system, as it was done in other parts of Europe. All of this increases the disaffection of the Church with these "newcoming democrats".
Now, to that context of absolutists claiming to be defenders of the faith and the Church privileges and wealth, and to the absolutists getting a strong support from the Church, add the apparition of Socialism and Anarchism in the mid 19th century.
Socialism and Anarchism (and anything suspicious of it) were met with frontal opposition from the Church8. So people with reformist proposals were often fiercely attacked by the Church, even if they were not anticlerical themselves. At the same time, the only thing that the Church demanded to the rich people was charity8 which, of course, was voluntary, and it was a secondary concern in relation to the fight against secularism and socialism/anarchism.
So, the idea that the Church was in league with the rich and powerful to control the masses8 again and protect its own privileges found a fertile ground. That again gave rise to an anticlerical sentiment; the expression of which10 only increased the determination of the Church on fighting the reformist movements which in turn made the Church an enemy of such movements.
1For example monasteries were entitled to a tenth (diezmo) of whatver the farmers in their region produced, but the diezmo of the richest farmer would go towards the Crown.
2Of course, that is hardly exclusive of the Catholic Church. Protestants did adhere too to the principle of Cuius regio, eius religio.
3Some of them were priests themselves asking for reforms.
4Finish off the Inquisition? Freedom of cult? HOW DO THEY DARE???
5While Isabella was something of "liberal" Queen, it seems to me that she was not particularly liberal and her actions were based in her need of liberal support.
6Including priests commanding military units
7Thanks to privileges and inheritances (in exchange of salvation of the soul of the giver), the Church was a very important landowner. Worse, many properties were left unattended because the Church had no incentive in exploiting them and were in the hand of small communities.
8Religion is the opium of the people.
9The first official position of the Church on the plights of the industrial workers and farmers was De Rerum Novarum in 1891, and while it sanctioned trade unions and collective bargaining, it also expressed support for private property and called for charity.
10For example, attack on church property and people during social unrest, as in the Semana Trágica in Barcelona 1909.