While not a military organization, as Jean-Luc Picard suggested, Starfleet is still an organization steeped in present-day military traditions. That is obvious from the traditional military parlance, the uniforms, and the rank insignia. More on that later.
A very sore subject, especially among those who don’t acknowledge Enterprise as part of the canon, is the existence of a pre-Federation Starfleet vessel called Enterprise and whether that is considered a continuity error. James Kirk’s ship was the first Federation Starfleet vessel to bear the name Enterprise. At no point during Enterprise (as far as I can tell) was Jonathan Archer’s Enterprise ever referred to as USS Enterprise or Starship Enterprise, but rather Earth ship Enterprise. Similarly, there’s been a first seafaring vessel named HMS Enterprise, a first seafaring vessel named USS Enterprise, a first aircract carrier USS Enterprise (the eighth US naval vessel of that name overall), a first space-shuttle Enterprise, etcetera. There was even an interstellar XCV class Enterprise that probably predated the NX-01–seen only in artistic renderings. In any case, we can safely say there was a Starfleet before there was a United Federation of Planets. The year of Starfleet’s founding is unknown, but perhaps can trace its origins back to the 2030’s when the NASA-owned explorer ship Charybdis, under the command of United States Air Force Colonel Stephen Richey, was launched . With that in mind, the National Aeronautics and Space Administration of the 2030’s was one of many organizations under the jurisdiction of other various nation-states (i.e. Great Britian, Russia, China) that pre-dated Starfleet and the United Earth Space Probe Agency (UESPA).
UESPA was a name first used in the original series episodes “Charlie X” and “Tomorrow is Yesterday”–before the writers had settled on the name Starfleet. While not heavily used in subsequent dialogue of this series or its spinoffs, UESPA is certainly a significant government entity founded shortly after the launch of Zefram Cochrane’s first warp-powered space vessel. One of its earliest contributions was the launch of the Friendship One probe in 2067, two years after the assumed launch of SS Valiant (back when the franchise implied we’d be part of an interstellar community by the 1990’s just like in 2001: A Space Odyssey).
In effect, by the mid 22nd century, when the first Warp Five capable starship was launched, Starfleet was the space program and the MACO‘s were the military. Those differences were reflected in their respective uniforms. The MACO uniform resembled Army combat fatigues while the Starfleet uniform resembled a NASA flight suit, right down to the mission patch. Following the Xindi attack on Earth in 2153, a unit of MACO’s was assigned to the Enterprise, an arrangement analogous to Marine detachments assigned to US Navy ships and aircraft carriers.
How the Xindi Incident and the Romulan Wars influenced Starfleet’s mandate in the UFP’s early years is uncertain. Federation politicians were understandably spooked by these conflicts, and that was reflected in the hierarchyl of Starfleet as the Federation’s military arm. By the 2230’s, however, with these threats having waned, Starfleet shifted most of its focus back towards exploration. During this era, the uniforms closely resembled the casual attire of crewmembers on the International Space Station (different color polo shirts to indicate affiliation with either STS-131 or Expedition 23, as well as differing mission insignias). The latter was practiced through most of the 23rd century with each starship having its own insignia. Uniform design had also begun to emphasize certain military designs starting with stripes denoting rank on the cuff of the sleeve, as seen on a US Navy service jacket.
The late 23rd Century saw a change to a more militarized style of uniform jacket, resembling a standard US military service jacket. At this time, the Federation had neither fought in any major wars nor stood on the precipice of war. Perhaps the narrowly averted war with the Klingon Empire in 2267, along with a re-emerging threat from the Romulan Star Empire, put Federation politicians and Starfleet brass on alert. With the Klingons vowing that “there shall be no peace as long as Kirk lives”, the Federation Council had every reason to suspect that either peace or war was on the horizon. Peace did come in the form of the Khitomer Accords of 2293, but the Starfleet brass still remained on high alert out of concern that weapons of great destructive potential would fall into the wrong hands (as was feared after the Cold War ended) and that the Romulans would make every effort to undermine a possible Fedeation-Klingon military alliance. Such a situation did come to a head during the Tomed Incident in 2311. The result was the Treaty of Algeron, which redefined the Neutral Zone and imposed a prohibition on the Federation developing cloaking technology (a seemingly one-sided agreement, but the Romulans most likely gave up something big in return, not mentioned in canon–metagenic bioweapons, singularity explosives).
Even with possible conflicts with these perennial enemies defused peacefully, the Federation faced the possibility of all-out war with the Tholians, the Cardassians, and the Tzenkethi. Over time, hostilities remained confined to the border regions of these hostile empires and Starfleet could start to shift more of its focus back towards exploration. And by the latter half of the 24th Century, Starfleet uniforms once again resembled NASA flight suits, albeit composed of a thinner fabric. This style remained in place until Q’s warning that the Romulans and the Klingons were nothing compared to what awaited humanity came to pass.
By 2373, the Federation was faced with a second Borg incursion, coupled with the prospect of war with the Dominion. These crises saw a return to a more militarized style of uniforms, that has remained in place after the war’s end. Federation military policy of the near future is uncertain, but in light of the very first major conflict where the very existence of the United Federation of Planets hung in the balance, the powers-that-be will remain far more alert than at any other point in UFP history. At the same time, UFP politicians have taken to heart President Dwight Eisenhower’s warning of a growing military-industrial complex.
Starfleet’s prioritization of interstellar exploration and militarization can be simplified down to the most basic explanation of opportunity cost. A nation’s economy devotes its resources to producing guns, butter, or some combination of the two. Producing more guns means sacrificing butter and vice-versa. So far, Starfleet has been very adept at deciding on the right combination of guns and butter.