If a few generals who sided with the president, they would face a lot of resignations all the way down the command change.
In Germany in the 1940s, Hilter got control by literally assassinating the generals who opposed his takeover to take control. But even that would have likely failed without the massive economic crisis caused by the punitive settlement of WW I and without the unusual characteristics of German society. (no, Trump is not Hilter and the US in 2020 is not Germany in the 1930/40s.)
We have tested it a couple of things in the US. Truman recalled MacArthur for attempting to publicly influence political opinion. I forget about the others.
So... the mostly likely thing if it is close is that it takes a while but the political establishment throws it to Biden, Trump sulks into the sunset (maybe even prison eventually), and the Republicans don't care because they have just got a 6-3 on the Court for a generation.