The 101-level crash-course on this is CGP Grey's video Death & Dynasties: https://www.youtube.com/watch?v=ig_qpNfXHIU.

In est, your "social connections" — Grey's "keys to power" — value stability of their own powerful positions in your government (or noble house, or family-run utility company/industry monopoly, or whatever other forms lumps of power can take.)

The key-holders' own political power exists regardless, but whether it translates to active ability to affect change right this moment depends on favor of the current ruler. Who better to place their bets on, then, as a replacement for a ruler who will continue to favor them — a ruler who will ensure the stability of the previous ruler's power base — than someone the previous ruler has been grooming for that very job from birth?

But I would argue that, at least for non-totalitarian states, there's also another, more interesting and crucial influence on what makes power legitimate.

When governments and noble houses generate revenue and get things done through free people who they employ or contract — not slaves or serfs or indentured servants — then it's the opinion of those free people on who is the legitimate next ruler, that actually determines who the legitimate next ruler will be. In a non-totalitarian state, a ruler cannot rule without the will of the people. To do otherwise provokes a populist-led revolution to abolish the seat of power altogether.

Looking at how lines of succession of royal seats of power work/are calculated can be enlightening, because there's a certain point where the rules cross over from "what anyone actually a part of the current royal house would want" into "what the population thinks makes someone a legitimate heir."

(The particular thing the population thinks makes someone a legitimate ruler, is usually a result of a centuries-long propaganda campaign by those in power; but no individual who wants power can entirely overwrite that belief during a succession crisis, which is the important thing here.)

Here's the way this works for the UK: https://www.youtube.com/watch?v=46N-bulO-aM

Note in the above, the people that get referred to as "royals" and have little crown icons. Those are the people that the existing ruler grooms as potential replacements, trying to get them established in the minds of their power base.

But note how there are so many other candidates to succession outside of this small group. These other candidates are there not because the royal family would rather transfer power to them, but rather because the will of the people in this case is to follow this weird rule (patrilineal primogeniture) wherever it takes them. (Which is a kind of rule-utilitarianism, in the sense that a society notoriously following this rule wherever it may lead, tends to result in the fewest wars of succession.)

If Westminster gets hit with a nuke one day, and all the current UK "royals" die — and then some con-artist pops up who was living in Morocco, and claims that they're the secret son-by-marriage-twice-removed of the Earl of Sandwich — then what that person is trying to do, is to claim legitimacy in the eyes of the people. They don't hold any of the current "keys to power"; but they think they might be able to step into those relationships and be accepted by those "keys to power", if they can first get the people who work for those key-holders seeing them as the key-holders' new legitimate boss.

This is also true when deciding who initially forms government in a feudal/contested state. Who "won" the War of the Roses, between the houses of Lancaster and York? The entirely-separate House of Tudor. The houses of Lancaster and York, through their violent conflict, ended up killing all the groomed male heirs of both houses — making them both invalid choices for succession in the eyes of the people (because patrilineal primogeniture), and in the eyes of the "keys to power" (because no established relationships left with anyone in those houses.)

And yes, this all still applies even in a country with democratic elections.

Most modern democracies are templated off the Westminster system, and so don't directly elect a president, but rather elect a legislature who in turn appoint a Prime Minister, like a corporate board of directors appointing a CEO. Guess who's getting appointed? Someone with established relationships with keys to power; perhaps hereditary ones. (Consider: Justin Trudeau.)

For countries that directly elect a president and have term limits, the choice might seem to be more in the hands of the people... and often is, at the country's founding. But you then get a primacy of political parties as noble-house-esque government-in-waiting entities, each trying to find and groom politicians into figureheads for the "party line", such that it's actually the party, and not the individual, that establishes the continuous key-holder relationships and carries them forward. The political party acts in lieu of a noble dynasty as the immortal entity conferring stability-of-power to key-holders.

The one way in which political parties aren't just noble houses, is that they will sometimes voluntarily allow outside entities who don't "toe the party line", to come in and take over for a bit — if those outsiders hold their own lumps of power. For a noble house, this would be suicide—they'd be "overwritten" by the outsider's new dynasty. But a political party will continue on just as they were afterward... but now having absorbed and digested the key-holder relationships that the outsider brought with them, into itself. (Consider: Donald Trump.) Though, note, noble houses do absorb external key-holder relationships — they do it through political marriages.