Spain's 19th century is extremely turbulent, and all the things that happened made Spain mostly irrelevant in the World stage.
Let's start at the beginning: Dixit Deus "fiat lux" et lux facta fuit. OK, maybe not that long ago. In 1776 the War of American independence began, and both Spain and France were major powers behind it and helped it happen. The 13 colonies obtained independence with the Treaty of Paris of 1783, and this independence sparked a whole idea of liberalism and independence in the Spanish America. Then came the French Revolution, in which a few people from the Spanish America took part, like Francisco de Miranda, the father of Venezuela. The French Revolution started a whole series of wars that spanned the whole continent of Europe for some 20 years, and part of them happened in Spain.
Come the year 1808, and through some very complicated scheming, Charles IV of Spain lost his crown in favour of his son Ferdinand, who in turn eventually abdicated in favour of Napoleon I Emperor of the French. Napoleon, in turn, made his brother Joseph king of Spain, and that's where things started to go South. A whole popular uprising started, with part of the Spanish army siding against the French, and wanting Ferdinand back as king, as among other things he was Spanish, not some foreigner imposed by another bloody foreigner. There were 6 years of war and guerrilla warfare. During the fourth year, the Cortes (parliament) gathered in Cádiz drafted and approved a Constitution, known as the 1812 constitution, the Cádiz constitution, or La Pepa. This constitution limited the king's powers, and made the executive power a matter shared between the king and the parliament. The king swore it because he did not have any other options. When the war was won, he abolished it and back came the absolutism.
With this war in place, there was some power vacuum in the Spanish America, with seriously conflicting loyalties. In 1810, a priest in Dolores (México) made a proclamation in favour king Ferdinand VII, but it soon turned into something entirely different: the War of Mexican independence. With the Spanish troops in Spain being too busy fighting against the French, the viceroy of New Spain could not expect reinforcements. In that same year, more uprisings were underway in the Upper Peru. No sooner said than done, the whole Spanish America was up in arms, with the invaluable cooperation (but not free of cost) of the British.
Spain could not deal with the multiple wars in the Spanish America, plus the war against the French. When the War of Spanish Independence ended in 1814, Ferdinand VII abolished the constitution and restored absolutism. This lead to many liberals being imprisoned, and many more exiled. Within the next years there were military uprisings in Spain trying to bring back liberalism, but all were repressed, until the year 1820. The troops that were going to be sent to fight in the Spanish America rebelled under the command of colonel Rafael del Riego in Las Cabezas de San Juan, proclaimed the constitution of 1812, and Ferdinand VII was forced to restore it. The constitutional rule lasted for 3 years until the army of the Holy Alliance came to Spain with a force of 100,000 men (known in Spain as "the 100,000 sons of Saint Louis, because it is much more polite to say that than saying "the 100,000 sons of bitches"), and restored absolutism again. It lasted until Ferdinand VII's death, a period known as The Ominous Decade. But things only get started now.
With the death of Ferdinand, the heir was his then 3-year-old daughter Isabel. This was not taken well by Ferdinand's brother Carlos, who thought he was the legitimate heir. He was also an abosolutist. So, in 1833 we have the First Carlist War, between the carlist and the cristinists, a civil war that lasted for 7 years.
The war being over does not mean the country would go back to normal. There were several coups d'état by different generals trying to impose their views, and that is something crippling to the economy and to the country itself. Power varied between the dictatorships of Narváez, the presidencies of O'Donnell, Espartero, and others. We even had a cabinet that lasted for one only day. In 1846 there was another carlist uprising, but of much more limited scale, that lasted for 3 years.
In 1868, with all the corruption and mismanagements of the Crown and their ministers, revolution erupts, the Glorious Revolution. A provisional government was formed by the leaders: generals Prim and Serrano, and admiral Topete. Then the Parliament started debating on who should be king: Isabel had just been ousted, her son was definitely not an option. The duke of Montpensier not only had very bad press, but had killed one of the Queen's cousins (also a cousin of his) in a duel, so another option flies out the window. A German prince could have been an option, and Leopold von Hohenzollern-Sigmaringen was considered, but France strongly opposed it. In late 1870, an agreement was reached, and Amedeo, Duke of Aosta, would become constitutional king of Spain.
Amedeo's joy did not last, his main proponent was murdered in late December of 1870, and the first thing he had to do when he set foot in Spain was attending his funeral. Things only got worse from there. In 1872 another Carlist uprising started, and by 1873 Amedeo called it quits with a speech that was tantamount to "fuck all of you, you are all insane, I'm leaving". Then came a Republic that lasted for one miserable year and four presidents, one of them calling it quits with the phrase "Honourable gentlemen, I'll be frank: I'm fucking sick of all of us" uttered in the Council of Ministers.
You can see that with that much instability and that many civil wars, Spain entirely lost the point of colonialism and the status as a World power. The Spanish-American war was just the last nail in a coffin already closed. In the 19th century Spain had 14 years of civil wars, 6 years of a large war being fought on Spanish territory, over half a dozen regime changes, and some twenty coups d'état. That will cripple any country.
TL;DR: A scarred country after a long war civil-warred and couped itself into irrelevance
The decline of the Spanish Empire was a long and slow that happened over centuries, looking for a single cause is a fools errand but in very broad terms, the Spanish Empire died as all empires do, due to over-reaching, mismanagement, corruption and wars. If you want to get in depth, I suggest J.H. Elliott's The Decline of Spain - Past and Present. The book must be close to 50 years old by now but at least in my opinion, it's still good.
Most historians trace the decline of Spain to somewhere in the first half of the 17th century. Some start with massive inflation that was triggered by the increase of the value of the Spanish copper coins in 1636, others with the Portugese and Catalan revolts in 1640. In either case, around this point Spain starts suffering its first serious setbacks that will lead to Spain ceding serious territory for the first time. Namely to the French.
I won't go into details about the thirty years war here but suffice to say it was a catastrophically expensive affair for everyone involved but for Spain in particular who were also suffering from the problems of provinces in open armed rebellion. All of this led to Spain doing what cash strapped empires do, borrowing heavily, minting new coin, mucking about with the monetary system... all the usual things that are the economical equivalent to pissing your pants. A very short lived relief followed by a nasty inevitable inflation.
Whereas most of the other major players of the 30-years-war bounced back eventually, Spain continued to suffer problems. Spain had suffered declining population and a corresponding general decline in industrial output and taxable income for a few decades due to various unpleasant plagues, draughts, famines and such. When the war finally ended, this continued to plague Spain which was particularly sensitve due to the massive manpower requirements of its vast colonial empire.
You have to understand that the Spanish Empire was always tremendously expensive to maintain. It was worth doing so because the profits from it were, at least initially, equally large. However, after the war the wealth generated from the colonies was severely reduced. The amount of silver reaching Spanish shores dropped to a fifth of peak levels, piracy meant more warships needed to be kept active in the caribbean and unruly local populations in New Spain meant that an armed presence was required at all times merely to keep the Empire intact. Whenever Spain was hard pressed at home or in a specific hotspot around its Empire and moved troops there, a new situation arose elsewhere.
The 18th century wasn't much better for Spain. Losing the 7-years-war didn't help. Indepence movements were beinging to form and gain strength in several places at once in no small part due to the Bourbon reforms. Then comes the Napoleonic war which further sapped both Spanish strengths and coffers and forced Spain to cede more territory to France.
When we get to the 19th century and the Mexican war of Independence the Spanish Empire is essentially a mortally wounded creature too stubborn to die. The loss of Mexico, Chile and Peru at basically the same time meant that Spain lost what remained of its colonial resources and revenues. By the time of the Spanish-America war there was precious little left of Spain that could be called an Empire. So here we are, at the beginning of the 20th century. I wish I could say that things improved for Spain from here on but... you know, civil war, fascism, dicatorship. Well. You did ask me to stop at 1900 so...