The War of the Spanish Succession took place because of a dispute over the rulership of Spain, and, more specifically, what certain changes to it might mean for the balance of power in Europe as a whole.
The Habsburgs had controlled the Spanish monarchy for a hundred years, but eventually a particularly weak monarch in a line of weak monarchs, Charles II, remained childless and then decided to give his throne to a French relative of his who was in line for the French crown as well. This would be ridiculous on it's own, but the Hapsburg's also had a long-standing conflict with the French.
Left unchallenged, this would result in the combination of France and Spain's power, which would be a devastating move not only for the collapsing Habsburg Dynasty, but for all of Europe. No ruler in Europe wanted the two monarchies to unite, but they also couldn't agree about how to divide them—and, anyways, the Spanish didn't want to be divided—so a thirteen-year war took place until it got figured out.
The war eventually resulted in the Spanish ruler giving up his right to the French throne so that the monarchies wouldn't be combined but Spain also wouldn't need to be divided.