What caused the decline of Spain after the colonization of the Americas?
As is usually the case with the decline of empires in history, there were multiple factors at play, often at the same time.
Though the Spanish Empire was at the height of its wealth, power and territorial extent during the reign of King Philip II Habsburg (r. 1558-98), his reign also saw several cracks starting to appear that would grow and grow over the coming decades. An excessively abundant import of gold and silver from Spain's American colonies made the country extremely wealthy in the short-term, it also dramatically raised inflation across the Spanish Empire, which over the years increasingly degraded the monetary value of Spanish currency and would eventually lead to a series of bankruptcies in Philip II's reign and that of his successor Philip III. This strained Spain's relatively primitive (i.e. feudal) economy beyond breaking point, and led to excessively large national debts, increased unemployment and a drop in foreign investment due to a lack of trust in the integrity of Spanish finance. The army would experience several cutbacks in both pay and size in general. This led to great difficulties in sending enough troops to the many different Spanish possessions around Europe (as far flung as the Low Countries and Italy).
Also during Philip II's reign, the Netherlands declared independence and begun a very long and very costly war of independence (1580 - 1640), and so did Portugal (and by extension its own colonial empire). Though neither of these wars would be concluded until the mid-17th century, they still forced the Spanish to devote enormous sums of money and reserves of manpower to deal with. A further foreign disaster came with the destruction of the Spanish Armada in the failed invasion of England, another huge blow to Spanish prestige and power projection.
Despite these setbacks, Spain was still arguably the strongest power in Europe. Their victory over the French in the Italian Wars (1494 - 1559) made Spain the undisputed master of the Italian peninsula for a century and a half, and the defeat of the Ottoman navy at Lepanto (1571) secured them as the primary naval power in the western and central Mediterranean. The first half of the 17th century saw Spain still as Europe's greatest power, although both of its long-running wars against its rebellious subjects (the Dutch and the Portuguese) ended in defeat for the Spanish. They were huge blows to both Spanish prestige and also its economy - the amount of trade revenue brought in by these two regions was absolutely enormous, and the eventual losses of both were irreplaceable.
France's victory in the Thirty Years War (1618-48) saw that country begin its resurgence and it was now essentially on an equal playing field to the Spanish. The long and exhausting Franco-Spanish War (1635-59) ended in a French victory, with Spain forced to cede several small but strategically valuable provinces to France. During the early stages of Louis XIV's reign the French army greatly surpassed the Spanish army in terms of both size and skill, as was evident in the conquest of the Spanish Netherlands (i.e. modern-day Belgium) by the French army in only a few weeks with complete ease during the War of Devolution (1667-8), though France was forced to return what it conquered when the English and Dutch declared war them.
The death of King Charles II Habsburg in 1700 without an heir led to the War of the Spanish Succession (1701-14), an enormous continental and global war between almost every country in Western and Central Europe. Charles passed the Spanish Empire to the grandson of Louis XIV, but inevitably almost everyone in Europe disputed this, most notably Austria, England and the Dutch Republic. Spain, ruled by Louis XIV's grandson Philip V, sided with France, naturally, but both countries were forced to make concessions at the peace negotiations in 1713-4. Philip V was forced to renounce all claims to the throne of France (so if Louis XIV and his son died, there would not be a union between France and Spain, as Louis XIV originally envisioned), and all of Spain's other European territories (i.e. the Spanish Netherlands and its possessions in Italy) were ceded to Austria, who basically took Spain's place as one of the leading great powers of Europe. Spain also lost several Caribbean colonies to the British.
The first half of the 18th century saw several attempts by Spanish kings to reconquer the lands lost to them in the previous century and a half, such as Italy and Portugal. Most of these met with failure, and it was clear to many by the mid-18th century that Spain was no longer a leading great power in Europe. Even the relatively small and poor country of Prussia was more militarily and economically efficient than Spain. Spain had lost a lot of territory in Europe, its military was weak relative to other European powers, and economically it was backwards and primitive. Several Spanish kings would try to modernise the country in various ways, but they always met with much opposition (from both the nobility and the Church) and often little success. In most cases it was also a matter of too little, too late. It's a bit pointless to continue the explanation to the French invasion of 1808 and the independence of its American colonies in the 1810s and 1820s, since Spain had already fallen from grace long before that, and was even at that time a shadow of its former self. The real decline occurred in the second half of the 17th century and the first half of the 18th century.