In most militaries, all assignments have a narrow range of ranks allowed. In Star Trek TOS, the characters were all depicted as having ranks suitable for their position. However, by the later movies, Scotty was a Captain, yet still chief of engineering, and Chekov and Uhura held the same posts on the Enterprise-A as commanders, as they did as mere lieutenants.
It was and may still be standard naval practice for the actual captain of a ship to be "Captain" even if they do not hold that rank, c.f. the Hornblower books. The writer's approach here may be based more on the literary version of this rule, rather than the actual practice in the 60's.
Today, commissioned officers that do not get promoted usually leave the service, so long-serving Star Fleet officers would have higher ranks for the same kind of reasons even if their duties/postings did not change much over time. There were plenty of gaps in the timeline for lots of other postings, commands, etc.
Also, Star Fleet may have adopted labor union style practices that allow the more senior members first pick of work assignments, something our military usually does not do.
The US Navy uses a series of Ranks and Ratings, your Rank being your level of authority and paygrade and your Rate being the specialty or job you perform. Thus a Tactical officer can be RANKED Lieutenant and be RATED as either Space Station Security of a backwater moon or Chief Weapons officer of a Galaxy-class cruiser. Most duty RATINGS would have a minimum RANK required, as (obviously) you cannot be THE Captain until you are at least A Captain.
Using this knowledge:
- Uhura is still a Comms Specialist by RATE, though in her career she has advanced from Lt. through Lt.Cmdr. and is now a full Commander by RANK.
- Admiral Kirk was demoted in RANK from Adm. to Cpt. and was allowed to maintain his RATING as Captain of U.S.S. Enterprise.
- Ben Sisko assumes command of DS-9 as a Commander, but is later promoted beyond it for service in the position.
"Captain" is both a rank and an indication of job/position on the vessel (so an honorific), which as commented by others can lead to confusion at times when addressing people.
As to rank, I think we can envision a society in which the ship sees many promotions during her extended tour of duty (in TOS, it's called "her 7 year mission", then "her continuing mission" in the movie). During that long tour, the crew would not normally rotate out for replacements but stay together unless there were an emergency. Thus over time people would hold positions (due to promotions) that with a fresh crew would be held by more junior personel.
We see this in the later movies when the original crew gets back and, older and at higher rank, and finds their stations taken by other people (who eventually get shoved out of the way, the normal job for an extra, as Kirk demands his old crew by his side for the mission assigned to him).
See this answer to a related question: How long does it take for someone to get promoted in Starfleet?
The gist of it is that the TOS "era" of the Star Trek canon (including the original series itself and all movies that feature the Enterprise under Kirk's command) take place over a much longer time frame than most other series, even TNG. The TOS era spans over 20 years (the Enterprise herself was 20 years old when she was destroyed in ST III). It would be extremely odd if anyone on such a decorated ship and crew were NOT promoted over that time; they would probably have left Starfleet for one reason or another. Kirk's promotion and then demotion is odd, but well-explained through the movies.
In addition, we're used to seeing the Enterprise bridge crew together, but over the various movies several of the crew have branched out. Pavel Chekov started ST II as the XO of the USS Reliant. Spock spends time in ST VI as special ambassador to the Klingon Empire, and Sulu captains the Excelsior in that movie. Most of the crew are split up and reassigned as of the second act of ST III before they join back together to steal the Enterprise.
The desire of the Enterprise crew to stay with Kirk, even as rank progresses to the point where other members could have their own commands and further promotions would be hindered by staying, is very simple; being on board the Enterprise is a much more interesting and fulfilling career than anything else Starfleet has to offer. Would you rather be off "boldly going where no-one has gone before", or cataloguing almost-lifeless planet after almost-lifeless planet looking for a suitable candidate for the Genesis Project?