(written from a Production point of view)
The "Theme from Star Trek" (originally scored under the title "Where No Man Has Gone Before"  and also known informally as the "Star Trek Fanfare") is the instrumental theme music composed for Star Trek: The Original Series by Alexander Courage. First recorded in 1964, it is played in its entirety during the opening title sequences of each episode. It is also played over the closing credits, albeit without its signature opening fanfare.
During the opening credits, the theme's opening fanfare is accompanied by the now-famous "Space: the final frontier" monologue spoken by William Shatner (with the exception of the pilot episodes, "The Cage" and "Where No Man Has Gone Before"). Throughout the opening credits, the theme is punctuated at several points by the USS Enterprise flying towards and past the camera. These "fly-bys" are accompanied by a "whoosh" sound effect created vocally by Courage himself. (Documentary: Music Takes Courage: A Tribute to Alexander Courage)
Conception and original use
Creator Gene Roddenberry originally approached composer Jerry Goldsmith to write the theme for Star Trek. Goldsmith, however, had other commitments and instead recommended Alexander Courage. (Star Trek: The Motion Picture (The Director's Edition) commentary)
Courage was not a science fiction fan, referring to the genre as "marvelous malarkey." He thus saw the theme he was writing as "marvelous malarkey music." Courage composed, orchestrated and conducted the theme in one week. He drew inspiration from a Richard A. Whiting song he heard on the radio as a child called "Beyond the Blue Horizon". This song had a drawn-out tune with a steady, fast-paced beat underneath it which Courage emulated when composing the theme. (Documentary: Music Takes Courage)
The theme used in "The Cage" – the unaired first pilot – featured a wordless melody line by soprano Loulie Jean Norman supported by electronic underpinnings. When a second pilot was ordered and the series was picked up, Norman's vocalizations were dropped from the theme.
The first season of The Original Series used two versions of the theme. On the original NBC and syndicated runs, five episodes – "Where No Man has Gone Before", the second pilot, along with "The Man Trap", "Charlie X", "The Naked Time", and "Mudd's Women" – used a mixed electronic/orchestral arrangement for the opening credits, with the main melody line created electronically and accompanied by more traditional instrumentation, including a flute and an organ for both the opening and closing themes. When the series was remastered for video in the early 1980s, only "Where No Man Has Gone Before" retained this version of the theme over both the opening and closing credits, while the opening was restored to the other four episodes and placed on five others when the series was remastered again for DVD release. The closing credits for the other nine episodes, however, used a version that had only an orchestral arrangement. The mixed arrangement was first heard on "The Corbomite Maneuver" (the tenth episode aired, although it was the second episode produced), after which the show opened with the orchestral-only arrangement.
Vocalization and lyrics
When originally written (and as heard in "The Cage"), Courage had Norman's vocalizations and the various instruments mixed equally to produce a unique sound. According to Courage, however, Gene Roddenberry had it re-recorded with Norman's accompaniment at a higher volume above the instruments, after which Courage felt the theme sounded like a soprano solo. Roddenberry's version can be heard during the opening credits of each episode in the second and third seasons; Courage's version is heard during the closing credits.
Further souring the relationship between Roddenberry and Courage, Roddenberry wrote lyrics to the theme without Courage's knowledge – not in the expectation that they would ever be sung, but in order to claim a 50% share of the music's performance royalties. Although there was never any litigation, Courage commented that he believed Roddenberry's conduct was unethical, to which Roddenberry responded, "Hey, I have to get some money somewhere. I'm sure not going to get it out of the profits of Star Trek."  Although the lyrics were never included on the series, they have been printed in several "TV Theme" songbooks over the years.
Portions of the Theme from Star Trek have been used in all 13 Star Trek feature films. Most of the Star Trek films' opening themes start by quoting the opening fanfare from Courage's theme, before seguéing into the film's own theme. However, there are multiple exceptions to this tradition. Star Trek: The Motion Picture did not use the fanfare at all in the opening or closing music, although a subdued version of the Theme from Star Trek was created by Courage at the request of the film's main composer, Jerry Goldsmith.  This arrangement of the theme was used for the "Captain's Log" cues. The theme was quoted again in Star Trek III: The Search for Spock and Star Trek IV: The Voyage Home, most extensively in the final scenes.
Star Trek VI: The Undiscovered Country, scored by Cliff Eidelman, broke with the tradition again. The Theme from Star Trek did not appear in the opening music, although it was used towards the end. Star Trek Generations, scored by Dennis McCarthy, on the other hand, did use the fanfare in the opening credits (and extensively throughout the score) but it did not appear until the end of the main title music.
The score for Star Trek, composed by Michael Giacchino, again did not use the fanfare in the opening title music: instead, Giacchino subtly quoted the opening notes and various other Star Trek themes from past films throughout his score. For the end credits, a re-arranged version of the Theme from Star Trek, fully orchestrated and with The Page La Studio Voices accompanying the melody line, was used. This version was also used for the end credits of Star Trek Into Darkness and Star Trek Beyond.
The theme's opening fanfare was adapted by Dennis McCarthy as the opening for the Star Trek: The Next Generation theme (the remainder of which was an adaptation of Goldsmith's theme from Star Trek: The Motion Picture). Courage's original theme can also be heard in the Star Trek: Deep Space Nine episode "Trials and Tribble-ations", the Star Trek: Voyager episode "Shattered", and the Enterprise series finale, "These Are the Voyages..."
Courage's theme was re-recorded for the remastered Star Trek episodes, with Elin Carlson emulating Norman's wordless vocalization.
Star Trek: Discovery composer Jeff Russo included Courage's fanfare at the end of the Discovery main titles. The theme returned in full at the end of the Season 1 finale, "Will You Take My Hand?", playing over the closing credits after the USS Discovery intercepts a distress call from the USS Enterprise.
In "Spock Amok", an episode of Star Trek: Strange New Worlds, at least a portion of the theme became diegetic (heard in the fictional universe) when a few notes of the fanfare could be heard on the PADD used to keep track of tasks for "Enterprise Bingo".
Other recordings and uses
TOS star Nichelle Nichols recorded a disco version of the theme. However, Nichols used different lyrics than those written by Gene Roddenberry. The late jazz musician Maynard Ferguson and his band also recorded a rendition of the song, a fusion version that was released on his 1977 album Conquistador. Ferguson's version was used as the opening theme for The Larry King Show on the Mutual Radio Network. The satirical rock band Tenacious D and the lounge band Love Jones recorded versions of the theme, as well, using Gene Roddenberry's lyrics.
The 1992 Paramount Pictures comedy Wayne's World was the first non-Trek film to use Courage's theme. In the film, the character of Garth Algar (played by Dana Carvey) whistles the theme while he and Wayne Campbell (Mike Myers) lie on the hood of Wayne's car, looking up at the stars. When Garth finishes the tune, he tells Wayne, "Sometimes I wish I could boldly go where no one's gone before. But I'll probably just stay in Aurora." The theme can also be heard in the films Muppets from Space (1999, starring F. Murray Abraham) and RV (2006, starring Robin Williams and featuring Brian Markinson).
At the 2005 Primetime Emmy Awards, TOS star William Shatner and opera singer Frederica von Stade performed a live version of the theme, with Shatner reciting the opening monologue and von Stade singing the wordless melody line.
In 2009, the theme was used as the wake-up call for the crew of mission STS-125 aboard Space Shuttle Atlantis.
For the 2021 inauguration of US President Joseph R. Biden, acclaimed cellist Yo-Yo Ma played the fanfare of the theme as a prelude to his performance of another song significant to Star Trek, "Amazing Grace".