To talk about Spain in the 19th century, we must talk about Spain in the 17th century. That is the period many agree is the beginning of the decline. And to talk about Spain's decline we must also talk about Spain's rise to become a European and global power in the 16th century.
Habsburg Spain
The Spanish empire of the 16th century happened mostly through inheritance, something already covered in several threads here.
To summarize, Charles V inherited separate lands, and separate kingdoms/counties/duchies in Spain, Burgundy, Low Countries, Italy, Austria and Germany. Spain still consisted of several kingdoms, each with its own rules: Castille and Leon, Aragon, Navarre. Castille had the most developed taxation system. Aragon was stagnating, Navarre was a new acquisition. Charles V put significant effort in extracting further revenue from Castille to finance his needs and commitments elsewhere. By contrast, he didn't put nearly as much effort with Aragon/Catalonia and Navarre.
Using Castille's reliable tax base, Charles borrowed huge sums of money from German bankers. This was used to set up strategic partnerships, such as with Genoa, to provide naval power in the Mediterranean.
The Casa de Contratación continued to control the Atlantic trade via Seville, and operated exclusively.
Philip II inherited Spain's kingdoms, Italian lands, Burgundy and the Low Countries. Charles V had cultivated a northwestern orbit between Spain, England (as Philip II was married to Mary Tudor), the Low Countries. However, as we know Elizabeth succeeded Mary Tudor and brought England away to eventually become an enemy of Spain. This made his efforts in the Low Countries more difficult and much more expensive, such that he declared bankruptcy multiple times. This, and inflations, means that Castille's artisan class couldn't develop and it remained a raw materials producer.
In the Mediterranean he had to contend with the rise of the Ottomans and their allies Barbary pirates. This means more expenses, reliance of Genoese navies and monies, just to maintain the link between continental Spain, the Balearic islands, Sicily, Naples. Because Castille was the cash cow, it was made to focus on sheep herding at the cost of crops, and Spain had to rely on Balearic islands and Sicily for grain shipments.
Philip III's reign was besotted by continuing financial stress, bubonic plague, and depopulation. Spain's armies started to have to rely on convicts more regularly to replenish its ranks due to population decline in Castille.
Philip IV relied on "favorites" such as Lerma and Olivares. While they had ideas of uniting the various kingdoms, they failed due to corruption and internal weaknesses. Conflict with France rose, and with that Olivares asked for Catalonia to contribute troops, funds, and housing in the Pyreneean border. Unhappy, Catalonia rebelled. Sensing an opportunity, Portugal, who was absorbed into a personal union under Philip II, also rebelled. The Thirty Years War and Eighty Years War saw Spain recognize the independence of the Netherlands in 1648.
Eventually, with Charles II the question of succession became paramount as he could not produce an offspring. The Bourbon candidate, descendant of Louis XIV, won the war of Spanish succession, ending the Habsburg line.
http://www.reddit.com/r/AskHistorians/comments/31zluu/the_decline_of_spain_in_the_16th18th_century/
At this point, it is important to quote J. H. Elliott's "Imperial Spain", which is that,
The reign of Charles V, in fact, saw three dangerous developments that were to be of incalculable importance for 16th and 17th century Spain. In the first place, it established the dominance of foreign bankers over the country's sources of wealth. Secondly, it determined that Castile would bear the main weight of the fiscal burden within Spain. In the third place, it ensured that within Castile the brunt of the burden was borne by those classes which were least capable of bearing it.
Throughout the reign of the Habsburgs, Spain's colonies in the Americas were limited to sailing only to or from Seville. Inter-colonies trade was not allowed. The manufacture of many basic and important sailing equipment was not allowed, the colonies had to purchase all these from Spain.
Bourbon Spain
Following his victory, the Bourbon Philip V was able to finally abolish several of the disparage states of Spain and further centralize power. Navarra, being an early supporter, was rewarded and was allowed to keep its fueros in name. Milan, Naples, Flanders, were ceded, so Spain became a power focused on the Atlantic.
However, there are still many problems. Spain still lacked a middle class. Even in shipbuilding they were considered weak. Capital was scarce, thanks to inflation and economic distress.
The Bourbon rulers of Spain instituted Bourbon reforms in the Americas, to improve administration. They founded new viceroyalties, allowed each to raise and maintain a militia, and reduced the influence of the church. New trade routes were opened, where previously routes were tightly controlled. A new administration meant the Bourbons encouraged continental Spanish administrators to move to the Americas, at the cost of conflict with the rising criollos. They also moved the Casa de Contratación from Seville to Cadiz, as the Seville port suffered from the river silting.
However, as we know these reforms came too little too late.
Sources
The 19th century was a very bad time for Spain.
At the beginning of the century, Spain was occupied by Napoleon. During this time, Caracas and Buenos Aires declared their independence.
The ensuing war for independence in Spain to oust France was highly destructive for the country and led to a rise in nationalism, as well as King Ferdinand's acceptance of conservative policies. He began to take measures to legalize an absolutist regime. The colonies especially didn't like this, as they enjoyed their somewhat autonomous rule. Cue the wars for independence. In 1814 they began, and Argentina was the first to succeed. Then came Chile, Colombia, Venezuela. By 1820 the only colonies left were Mexico, Peru, Ecuador, and Central America. Important revolutionaries to know are Simon Bolivar (led Colombia and Venezuela) and Jose de San Martin (led Chile and Argentina).
Meanwhile, on the homeland, the liberals and conservatives were still very much at odds as a result of Ferdinand's decisions. Once Ferdinand died and his wife, Maria Cristina, became regent for their daughter, Isabella, a pretender to the throne, Infante Carlos, second son of Charles IV and Maria Luisa, attempted to take the country. And so, in 1833, the Carlist wars began.
The Carlist army, after initial defeats, grew to a size of 30,000 troops and began having major successes. The Spanish government started to go deep into debt. In addition, the liberals were still unhappy with the current monarchy and rumors of a coup began to arise. With British weaponry, however, the Spanish government was eventually able to suppress the Carlist forces. Pressures from the liberals eventually won out, and the regent Maria Cristina accepted a new constitution that allowed parliament greater powers.
Without the previous income from the colonies, and the Carlist wars only ending in 1839, the government spiraled deeper and deeper into debt. To cope, they seized and sold monastic property.
Maria Cristina, no longer having to worry about the Carlists, attempted to undo the constitution that she had agreed to. This eventually led to her removal as regent. A military leader, Espartero, was named regent, though he proved to be authoritarian and overall ineffectual. After many resulting coups due to unhappiness with the regent, the courts decided they didn't want to put another regent in place. They claimed Isabella, aged 13, was of age to rule. That went about as well as you'd expect, constantly changing whose ideas she supported, making ineffectual decisions, and generally adhering to the actions and policies that made her mother so unpopular. Attempts were made at removing power from her hands.
Espartero, fresh from his return to power, was named President of the government. Not wanting to share power with others, he attempted to remove those who may oppose him by reviving the progressive movement. He failed, and ultimately his time in power resulted in nothing.
After a few poor decisions on Isabella's part, another rebellion. This time led by the political leaders in parliament. They were crushed, leading to widespread discontent. In an attempt to appease, Isabella flip-flopped between appointed officials. Isabella had managed to alienate liberals, moderates, and conservatives by her lack of consistency. In 1867 they all met to plan an uprising. In 1868, the navy staged a mutiny, much of the army defected to the rebels, the government was swiftly defeated, and Isabella fled to France where she stayed for the rest of her life. In 1869 a new, fairly liberal, constitution was adopted by the liberals, moderates, and conservatives, and in 1870 they chose their king to be Italian prince Amadeo.
After a short time in chaotic Spain, Amadeo declared the country ungovernable and abandoned the throne. This left a new Carlist uprising to fight the now kingless government.
Sources:
Spain in the Liberal Age: From Constitution to Civil War by Charles J Esdaile
Spain: A History edited by Raymond Carr