I think of the three trilogies the only one that wasn’t done by winging it was the PT. The end was know before we saw the TPM crawl. Vader turns, the Empire rises, Luke and Leia where hid after they were born. The only story point that in any way changed was when Padme died, and even that may have been set.
But the OT was done on the fly. George had a general story from the long script that he followed in TESB and ROTJ, but the deeper story was done on the fly. We don’t know when he decided to make Vader Luke’s father. I think it was very early in TESB story development, he just never put it down. It was not in any version of the script until the script was published later. Only 6 people knew before the paperback came out in April 1980. The final confrontation in ROTJ changed a lot as the drafts progressed.
The ST was done much the same. TFA was an intro. TLJ was all about character development. TROS not only had to be the finale to the trilogy but to the saga. Trevorrow had a creature who taught Palpatine… but wasn’t that supposed to be Darth Plagueis? Changing that to be Palpatine himself and unleasing a fleet of Star Destroyers carrying an updated and more compact planet killer was genius. It gave the movie an epic villain and an epic conflict. It gave Kylo competition and conflict. It was exactly the ingredient the final story of the saga needed. Some have complained that Palpatine came back and some have complained about bringing back planet killers yet again, but for anyone who has watched any version of Flash Gordon, you know that Ming never dies and always comes back and he always has worse weapons every time he comes back. Star Wars is supposed to be a more serious take on the camp of Flash Gordon. So I think Palpatine made the ending perfect. It ties all 9 movies together into a whole.
And I wouldn’t be surprised if the inspiration was Max Von Sydow. Abrams might have turned to Flash Gordon for inspiration and heard the movie end with Max’s evil laugh as Ming… after the heroes had won and Ming was dead.