The three divisions is more of a historical quirk if anything, pointing to a time when three divisions was helpful. The medical division and the security division may have been add-ons. Enterprise, for example, doesn't have a medical staff (Phlox being their lone doctor and alien observer) and this might imply that medical operations were done by civilians outside the rank structure. Enterprise had security (already tied in with the armory/tactical), but later introduced the MACOs. Perhaps earlier vessels of the era utilized the MACO agency as a separate security force, allowing Starfleet to continue operations under the legal fiction as a pure exploratory arm and successor to NASA/UESPA.
So, the three divisions of early Starfleet were the guys who maintained the ship (Red-Striped Engineers), the guys who fulfilled all the exploration and study mantra of Starfleet (Blue-Striped Scientists), and the guys who oversaw everything (Gold-Striped Commanders).
Perhaps on the earliest ships, the pilots were the COs, and eventually it was a fast-track to command (note: Archer was a pilot). When Security was folded in, they were placed in operations instead of creating a fourth branch, perhaps because it was a small force and they were usually dual officers overseeing the weapons equipment (i.e. also maintenance on the ship). Medicine is a science, so placing them into Blue-Stripes makes sense if they don't want to create a new scheme. ...But we never see a Medical officer in Enterprise, and the first ones we see chronologically are the Kelvin officers (in all white). By the Disco era, medical is generally still wearing white, but feature silver piping, indicative of scientists (and they are in Sciences Blue during The Cage and TOS era, and probably Pike's Disco-era variation).
The Enterprise era featured colored piping, the Franklin era had large shoulder epaulets, and the Disco era returned to the piping of Enterprise. It was the Kelvin era that first experimented with colored shirts, allowing much easier visibility of the divisions (they also changed division colors for the first time, a decision apparently reversed in subsequent eras for a time), which breaks a little of the uniformity of earlier eras and Disco. The Pike variation of the Disco era returns to colored shirts, while returning to the three division colors from the very beginnings of Starfleet.
The TOS era is a clear evolution of the Pike variation, dropping the unnecessary piping and streamlining alot of the structure, probably out of necessity when they adopted organic uniforms as opposed to the fabricated ones of Discovery.
The TMP era dropped the three divisions for the first time, adopting six: White (Command, indicative of the admiralty of the Enterprise era which I will get to), Orange (Sciences, an odd change), Green (Medical, accepted as its own division, and only slightly changed from the Blue before), Red (Engineering, carryover from TOS era), Gold (Flight control, carryover from before, even though command has "branched" out), Grey (Security, accepted as its own division and possibly alluding to its MACO origins or the greyish color from the Kelvin era). Further evolutions and switches were made in the Movie era/Lost Era, and the three divisions only return in the 2350s for the TNG era, adopting red for command (likely indicative of the red worn by all officers from at least 2278 to at least 2348), and maintaining gold for engineering (as it had been adopted for the Movie era), while folding security back in, and restoring blue for an overarching sciences division consisting of the formerly green medical and formerly grey sciences division.
That heirarchy of divisions has been retained through five or six uniform eras, from at least 2353 ("Suddenly Human") until at least 2399 ("The Impossible Box", I won't spoil any episodes released less than six months ago), a span of 46 years.
Admiralty in the Enterprise era wore their own division, red-and-white, which likely eschewed any specialties they had in their lower ranks and makes sense, if we assume division colors were mainly to denote shipboard assignments at a glance. The Admirals would be differentiated by their shoulder patches, most likely. In the Kelvin Timeline, the admirals seemed to retain the White color variation (this may have been the same as the Kelvin era in the regular timeline, even if it clashes with medical attire). In the Disco era, we see that admirals seem to wear gold piping indicative of the command division, with no indication of what non-command admirals wear. But the admirals and commodores of the TOS era wore different divisions (red and gold were seen), and this may have continued or been restored in the TNG era (with Vice Admiral Toddman) and Picard era (with Commodore Oh), both figures wearing operations gold and identified as Starfleet Security chiefs (although this flies in the face of command-red Vice Admiral Henry in The Drumhead). All admirals in the movie eras and early TNG (before Toddman) wore command colors (white or red).