King Charles II had always been sickly. He was the last Spanish King in the Habsburg line, and had no heirs.
On Nov 1 1700 he kicked the can; he had wanted Louis XIV of France's teenage grandson Philip of Anjou to succeed him to the throne, but a lot of Austrians had other ideas in mind. The Holy Roman Emperor wanted it for himself. Philip was crowned two weeks after Charles died.
War ensued.
https://en.wikipedia.org/wiki/War_of_the_Spanish_Succession
https://en.wikipedia.org/wiki/Charles_II_of_Spain
All that royal intermarriage had a lot of bad consequences.
Note that contemporary scholarship has gotten past all this great-men-of-history perspective and shows how the wives and daughters were really running thiings, and the economic and political changes were directing the royalty paths.