The first step is to declare the election fraudulent. The second step is to demand a "re-do". What next comes next depends on just how the election is re-done.
The Republican party can be classified as being in four main camps. There's the ones who are actively upholding the rule of law and opposing Trump. There are the ones who are not saying anything, and perhaps participating in Trump's agenda somewhat, out of fear of the consequences for standing up to him. There are those who have been bought into Trump's propaganda and honestly believe that the election was stolen. And there are those who are fully aware this is a scam, but are willingly going along with it because they see it as an opportunity to advance their interests.
How a "re-do" of the election proceeds depends on how much influence each of these groups have on it. The fourth group will be pushing voter suppression to the extreme, and if that's not enough to swing the election their way, they quite possibly will simply engage in fraud. The third group will largely go along with anything the fourth group can frame as fighting fraud. Much of voter suppression will fall in that category, and even creating fraud by Republicans can in many cases be twisted into fighting fraud by Democrats. The third group will be faced with a lot of cognitive dissonance, which in many cases they will have to resolve by either moving to the first or fourth group. If the fourth group can sideline the first group, cow the second group into going along, and manipulate the third group into serving their agenda, they may be able get the election to give the "correct" result, allowing Trump a patina of legitimacy.
On the other hand, if the third group are the ones running the show, they may buy into the fourth group's propaganda so much that they honestly believe that if they simply run the election in a completely "fair" manner, then Trump will win, and be surprised to find that Trump still loses. If the first group can take control of the re-do and assure the second group that they can act with integrity without too much risk, then they may be able to keep the election result from changing.
Where martial law comes in is that it allows Trump to sidestep what he calls the "deep state" and other people call "basic democratic institutions" and replace it with a top-down structure largely dictated by him. The top would be largely the fourth group, the middle the third group, and the bottom the second group. The first group would be declared incompetent/corrupt/treasonous and removed from their positions. If it comes to that, whether it succeeds depends on how much Trump can establish that hierarchy. Trump has exhibited incompetence in a wide range of fields, and there's little reason to think that pulling off a coup would be an exception.
There's also the question of how far non-Republicans and the first group will go to oppose this. They seem to have rolled over on the grabbing people off the street in Portland. The courts would almost certainly declare this illegal, but if Trump sends federal officers to carry out his orders, injunctions won't do anything unless anyone stands up to enforce them.
Also, it's not clear how much of Trump's campaign against the election results is actually directed towards overturning it, and how much of it is motivated by other concerns, such as radicalizing the base for Georgia's run-off, scamming more money from the third group (Trump has raised millions of dollars off of this), or just feeding Trump's ego.
As for legality, it would violate standard legal principles, but it is generally accepted that legal principles can be ignored in "special" cases. The Japanese internment camps, the draft, and "ceremonial deism" are all examples of the letter of the law has been ignored in favor of the alleged "spirit". And as Peter points out, if Trump succeeds in destroying the current democratic infrastructure, he can have it retroactively declared legal.