I'm much less concerned about Spock since, as Nerys pointed out, Spock entered the world of diplomacy. TNG turned him into an ambassador -- which makes sense since Unification/TUC were in overlapping production and TUC starts out with Spock as part of the special Federation envoy to the Klingon Empire. It's also one of the few TNG elements that lived on into the JJverse. So even though he's technically outside of Starfleet, *everybody* in Starfleet during the TNG era regards him as a legend.
It could very well be that while we Trekkies in the real world will always think of him as a First Officer, in-universe he could be much better known as an ambassador, which is a very, very accomplished, respectable feat.
Yeah but even in the period between TVH and TUC there were several years where he seemed to be more a regular line starfleet officer as opposed to his eventual shift towards the diplomatic corps. Why wasn't he apparently he offered his command during that period and just demoted back to first officer. Yeah I know the whole "loyality to Kirk" thing could be played as a card, but it just seemed like a demotion for no good reason to me.
This isn't quite explained, but it could be that Spock still needed some mental recovery and reeducation after the events of TSFS. TVH showed him undergoing the process, but it was interrupted by the trip back to Earth and then the whale probe incident (and then played up for comedy throughout the movie).
But then I'd only say that only really covers TVH - TFF. Between TFF and TUC, that's anyone's guess, but I think TUC implies that eventually he moved into something akin to Starfleet's equivalent of a part-time position for XO while jumping into diplomacy.
Or he could have seen what a shitty job the 3 diplomats on Nimbus were doing and decided to roll up his sleeves to show 'em how it's done.
Though, I think it's important to note that in the real-world US Navy, working hard to the rank of captaincy doesn't necessarily guarantee permanent command since there are only so many ships around. Indeed, there are many officers who rise above the rank of captain but don't command a vessel because that's either not where their careers take them, or they choose not to (and Starfleet, much more than the Navy, seems to allow for a lot more room for personal preference when it comes to career). So Spock could have stepped down from the position by his own choice as well. And it could be that Starfleet regards Spock as a good captain but an even better potential diplomat; where Spock is put to better use not on a single starship, but affecting large scale policy, so he could have been groomed during that time as well. But that's just speculation on my part.
And then somwhow was back in uniform for Riker's wedding.
Starfleet is a very forgiving place. First he commits perjury and partakes in a cover up involving the death of a cadet and then he defies orders and compromises a diplomatic mission, making a volatile situation worse, and then escapes punishment by giving his resignation. And yet a few years later, they take him back! Wow!
I'm going with the ironically less fantastic idea: that his abilities as a Traveller enabled him to make a uniform out of thin-air like Q.
I'm more inclined to believe that than the idea that Starfleet would take him back, at least.