Partially it's due to structural weaknesses in the spanish empire: It was made up of a bunch of different disparate units that only with difficulty could be manuevered into the same direction.
Really? That didn't harm the British Empire, during its heyday.
It's true that the Spanish empire was far-flung, that communications with the more remote parts of the empire were difficult, and that the royal government had trouble imposing its will uniformly throughout the empire. But Madrid was pretty good at extracting (some) taxes from the empire, and keeping overall political control.
That's more than you can say about the British empire of the 18th century, who tolerated (voluntarily? or because they had to?) the political autonomy of their North American colonies, and promptly lost them once the colonies thought they didn't need the colonies any more.
There was dissent in the colonies, but the governors kept a lid on that dissent, and kept sending taxes to Madrid, until Napoleon marched into Spain and overthrew the monarchy. Spain descended into war and political chaos, and only then (!) did the colonies go and overthrow the
Peninsulares, i.e. the Madrid-appointed governors and their hangers-on.
Without the Napoleonic War, Spain would never have descended into such chaos, and I don't see why the colonies wouldn't have remained in awe of the crown's powers and remained obedient for another 20-30 years. Provided no other war came along and ravaged Spain, of course.
The main problem for the Spanish, and the main reason why Spain stopped being a great power long before the lost their empire, was that the empire's vast distances meant that policing and defending that empire was very expensive. So expensive that the taxes levied from the empire barely sufficed to pay for it. Had the Spanish been better at promoting economic growth in their colonies, maybe they could have levied more taxes, and would have remained a great power... but then again, the colonies were very sparsely populated, and during those time economic growth was very difficult without population growth. Gold and silver mining yielded huge profits but the mines were exhausted. Even a better government would have had trouble in averting the decline of the Spanish Empire.