Starfleet is that enigmatic, "non-military" military which acts simultaneously as the organ of defense and security, diplomatic vehicle, and public front of the United Federation of Planets.
Though it's not entirely clear whether the representatives of the council of the Federation are democratically elected or otherwise appointed (the practices probably varying among member worlds), the organization of Starfleet is specifically and explicitly hierarchical. Though Starfleet Command maintains (when pressed) that it's "not a military organization" (which means that, according to the party line, the Federation has no military per se), Starfleet levels of seniority are named for traditional Earth military ranks, from Ensign through Lieutenant and Commander to Captain and Admiral, with some intermediary ranks and an alternate structure for noncommissioned officers. They carry and are trained in the use of phasers and other firearms, and their starships have extensive armaments which can even overshadow their scientific equipment and cargo bays (theoretically their actual raison d'être).
Starfleet was instrumental to the establishment of the Federation. Although the United Federation of Planets is supposedly a cooperative venture of many worlds, and draws on the scientific knowledge of its myriad member civilizations, its organization is based on human political and economic principles, and Starfleet is primarily a human enterprise. Humans had eliminated war and sticky problems of division of labor three generations before Starfleet's first flagship, Captain Jonathan Archer's Enterprise, was launched into a region of space full of conflict. The Federation was, to a large extent, the result of humans, fledgeling starfarers, bringing their ambition and wisdom to the self-superior Vulcans, aggressive and paranoid Andorians, and other founding races.
In the time of Captain Kirk, Starfleet was a peacekeeping frontier force, the perfect opportunity for a maverick to make a name for himself, so long as he preserved the integrity of Starfleet's Directives. Though armed engagements with enemies like the Klingon and Romulan empires would occasionally arise, the Federation was free from war. This was a time for Starfleet to consolidate its power in the name of mutual benefit, defense, and cultural and scientific exploration.
The Starfleet that exists four generations later, though, is a different story. While the organization presents an almost Egyptian austerity aboard its flagships, and maintains a semblance of peace with its ancestral myth of common interest, it is effectively a fascist state police. Vessels like the Enterprise help present an image of sophistication and help to get inquiring minds away from closed doors and into the frontiers, while increasingly totalitarian factions vie for control, all in the name of peace and order.
The reason for this intrigue and turmoil: – An early generation of Starfleet Command brought prosperity and peace to the Federation. Later Command found itself beset from all sides by rapacious outsiders intent on gaining riches or Lebensraum – or slaves. War after war wracks the Federation; the Romulans, the Cardassians, the Borg, the Dominion, the Borg again (and probably to come again as well), all intent on disrupting the Federation's hegemony over the Alpha and portions of the Beta Quadrant. Every few years, Starfleet comes inches from declaring martial law. The citizenry remains generally cowed; who would talk out against Starfleet, bastion of civilization?
Fortunately, despite its paternalism and power, Starfleet is remarkably free from corruption. It also contributes to a high standard of living throughout the Federation, thanks to judicious shipping and defense. Dissention isn't squashed, it just doesn't arise; few people outside the command structure see the threats of Starfleet's consolidated power, and those who see it usually try to engage it peaceably rather than taking on Starfleet itself. Exceptions exist.