A social contract is only legitimate if it is consented to and the people have the power. The main purpose of free elections or plebiscites is to enable citizens to renew the social contract, either through their elected representatives (Rousseau didn't approve of these) or direct democracy. Most of this idealism has long been perverted, subverted, and twisted, and only still appears as an idealistic vision to those who benefit from it or are too naïve to see through it.
This applies to all three principal areas of government, the legislative, executive, and judiciary. Owing to entrenched structures that are only 'reformed' for the worse, the power of elections to ensure the renewal of the social contract is much diminished, if it still exists at all.
As you're mainly concerned with the judiciary, we've already seen with the Supreme Court the destruction of the previously-existing 'gentlemen's agreement' on its composition and its politicisation, and we would be likely to see further erosions of the separation of the powers.
When you ask what legitimises the people in power in the judiciary, it is obviously firstly the law. It's en entrenched body of work and like a fatberg in a sewer is hard to shift. Much of it makes sense and quite a lot of it makes less sense, e.g. the existing legislation around road traffic collisions. With its many problems, it's still better than not to have it. 'This isn't a court of justice, son, this is a court of law.'
Secondly, they have all done a lot of work to understand the existing state of the law and possess skill. Even if you start out privileged, you can't just waltz into the profession without having put in the miles. Sure, they may 'just' be people, but you wouldn't know that Greg LeMond was a champion cyclist if you met him on the street or at a party and didn't know who he was, either. He might tell you, but then you'd still have to verify what he says by getting other information. Some professionals find it easier to produce evidence of their skill, e.g. bakers who bake good bread or cakes, and others find it harder, as the products of their work are harder to understand than bread and cakes, and lawyers are obviously in that latter camp, but ask anyone if they'd prefer someone who's good at their job or not, and they probably opt for good except if they're competing against that person. Needless to say, there are better and worse justices, although with the degree of specialisation required to practice law today, it's hard to compare them between their different silos. Skill, and the constant competition between prosecution and defence, is certainly another part of their legitimacy.
Finally, while what exists may be unjust in parts and frequently problematic, certainly if it's not cared for on a constant basis, and many people wish for the dismantling of existing structures, a vacuum/breakdown of law and order is not what you really want, as shown, say, in post-invasion Iraq, so a key part of legitimacy is that whatever their faults, existing structures are stabilising. It is when they head in too extreme a direction, e.g. the last four decades of worsening injustice, that their effect becomes destabilising.
Challenges to their legitimacy include that judges and lawyers are saddled with the state of the law as it is, with major factors in the stasis relating to renewing the social contract being the slow speed of the legislative today, political corruption and lobbying, etc., which is not judges' fault, as they are not the legislative (although in Anglo-Saxon judiciary the legislative and judiciary are not fully separated owing to precedent, e.g. see recent action in the US). They may err in interpreting the law, but that can be corrected on appeal (it not always is). Miscarriages of justice happen and the system is generally stubborn and glacial in acknowledging this, when it would probably help public perception to deal with cases like Andrew Malkinson's more quickly. There are poor outcomes in trials. Juries may be misled or there may be no jury, in which case the judge may not be misled as easily, but may more easily show bias. Not that juries always get it right. It is generally thought that the law favours wealthier over poorer people, especially with not-even-that-recent-any-more changes to legal aid etc., or see the McLibel trial for an example of Davids vs. Goliath.
The upshot of all this is that public perception can easily be instrumentalised. Trump and co. are appealing to (some) people's desire for radical change, i.e., the current state of society is such that whatever passes as the social contract (including all the inequality caused by private property, pace Rousseau) is not consented to by many people—the angry and dissatisfied.
Needless to say, Trump and co. are not proposing to give these people any power, so the latter half of the equation would not be fulfilled. (As above, in Rousseau's view representative democracy doesn't give people the power, only their representatives, so whether Trump or Harris wins, in either case his idea of the social contract would not be fulfilled.)
The only real difference is that Harris and co. would accept that the social contract is actively being consented to (as the Republicans used to, as well), and do not appeal to people's sense of injustice in the same way as Trump. When Trump says that if he loses the elections, they have been illegitimate, this is an expression of the perception by many of his supporters (driven both by genuine feelings of powerlessness and Internet propaganda) that elections today fail to solicit genuine consent and that they only keep ushering in more of the same.
(The only reason why he does this is because he is probably a coerced agent of a hostile foreign power trying to weaken the US, and because he is desperately unhappy and ill and has a long-entrenched mechanism stemming from emotional abuse to try to paint everything concerning him as amazing and brilliant. As has been noted ad nauseam, the irony of someone who started out very wealthy being set up to appeal to this is obviously very concerning.)
Instead, 'under him' you would be heading towards a further dismantling of the vestigial structures, long subverted by extreme inequality, that were originally at least ostensibly designed to ensure a better society (obviously, whether they were ever genuinely designed for that purpose is rather up for debate).
Have you read Rousseau? Specifically «Du contrat social»:
https://en.wikipedia.org/wiki/The_Social_Contract
In a nutshell:
A social contract is only legitimate if it is consented to and the people have the power. The main purpose of free elections or plebiscites is to enable citizens to renew the social contract, either through their elected representatives (Rousseau didn't approve of these) or direct democracy. Most of this idealism has long been perverted, subverted, and twisted, and only still appears as an idealistic vision to those who benefit from it or are too naïve to see through it.
This applies to all three principal areas of government, the legislative, executive, and judiciary. Owing to entrenched structures that are only 'reformed' for the worse, the power of elections to ensure the renewal of the social contract is much diminished, if it still exists at all.
As you're mainly concerned with the judiciary, we've already seen with the Supreme Court the destruction of the previously-existing 'gentlemen's agreement' on its composition and its politicisation, and we would be likely to see further erosions of the separation of the powers.
When you ask what legitimises the people in power in the judiciary, it is obviously firstly the law. It's en entrenched body of work and like a fatberg in a sewer is hard to shift. Much of it makes sense and quite a lot of it makes less sense, e.g. the existing legislation around road traffic collisions. With its many problems, it's still better than not to have it. 'This isn't a court of justice, son, this is a court of law.'
https://www.youtube.com/watch?v=IiSXGjVriUE
Secondly, they have all done a lot of work to understand the existing state of the law and possess skill. Even if you start out privileged, you can't just waltz into the profession without having put in the miles. Sure, they may 'just' be people, but you wouldn't know that Greg LeMond was a champion cyclist if you met him on the street or at a party and didn't know who he was, either. He might tell you, but then you'd still have to verify what he says by getting other information. Some professionals find it easier to produce evidence of their skill, e.g. bakers who bake good bread or cakes, and others find it harder, as the products of their work are harder to understand than bread and cakes, and lawyers are obviously in that latter camp, but ask anyone if they'd prefer someone who's good at their job or not, and they probably opt for good except if they're competing against that person. Needless to say, there are better and worse justices, although with the degree of specialisation required to practice law today, it's hard to compare them between their different silos. Skill, and the constant competition between prosecution and defence, is certainly another part of their legitimacy.
Finally, while what exists may be unjust in parts and frequently problematic, certainly if it's not cared for on a constant basis, and many people wish for the dismantling of existing structures, a vacuum/breakdown of law and order is not what you really want, as shown, say, in post-invasion Iraq, so a key part of legitimacy is that whatever their faults, existing structures are stabilising. It is when they head in too extreme a direction, e.g. the last four decades of worsening injustice, that their effect becomes destabilising.
Challenges to their legitimacy include that judges and lawyers are saddled with the state of the law as it is, with major factors in the stasis relating to renewing the social contract being the slow speed of the legislative today, political corruption and lobbying, etc., which is not judges' fault, as they are not the legislative (although in Anglo-Saxon judiciary the legislative and judiciary are not fully separated owing to precedent, e.g. see recent action in the US). They may err in interpreting the law, but that can be corrected on appeal (it not always is). Miscarriages of justice happen and the system is generally stubborn and glacial in acknowledging this, when it would probably help public perception to deal with cases like Andrew Malkinson's more quickly. There are poor outcomes in trials. Juries may be misled or there may be no jury, in which case the judge may not be misled as easily, but may more easily show bias. Not that juries always get it right. It is generally thought that the law favours wealthier over poorer people, especially with not-even-that-recent-any-more changes to legal aid etc., or see the McLibel trial for an example of Davids vs. Goliath.
The upshot of all this is that public perception can easily be instrumentalised. Trump and co. are appealing to (some) people's desire for radical change, i.e., the current state of society is such that whatever passes as the social contract (including all the inequality caused by private property, pace Rousseau) is not consented to by many people—the angry and dissatisfied.
Needless to say, Trump and co. are not proposing to give these people any power, so the latter half of the equation would not be fulfilled. (As above, in Rousseau's view representative democracy doesn't give people the power, only their representatives, so whether Trump or Harris wins, in either case his idea of the social contract would not be fulfilled.)
The only real difference is that Harris and co. would accept that the social contract is actively being consented to (as the Republicans used to, as well), and do not appeal to people's sense of injustice in the same way as Trump. When Trump says that if he loses the elections, they have been illegitimate, this is an expression of the perception by many of his supporters (driven both by genuine feelings of powerlessness and Internet propaganda) that elections today fail to solicit genuine consent and that they only keep ushering in more of the same.
(The only reason why he does this is because he is probably a coerced agent of a hostile foreign power trying to weaken the US, and because he is desperately unhappy and ill and has a long-entrenched mechanism stemming from emotional abuse to try to paint everything concerning him as amazing and brilliant. As has been noted ad nauseam, the irony of someone who started out very wealthy being set up to appeal to this is obviously very concerning.)
Instead, 'under him' you would be heading towards a further dismantling of the vestigial structures, long subverted by extreme inequality, that were originally at least ostensibly designed to ensure a better society (obviously, whether they were ever genuinely designed for that purpose is rather up for debate).
Anyway, it is something like this.