I think that Star Trek fits into a kind of literary genre that has been around for centuries: adventures in distant lands.
One of the oldest of these is Homer's Odyssey, where the eponymous hero has numerous misadventures in distant lands as he tries to get home. An early modern one was Jonathan Swift's Gulliver's Travels, a work that used such adventures as a way to do social commentary.
Such distant-land adventures were common in the pulp fiction of the late 19th cy., but they gradually faded in the 20th cy. as more and more of our planet became easily accessible.
But if one can't have adventures on our planet, then one can have them elsewhere in the Universe. One of the first was Edgar Rice Burroughs, who started his Barsoom series in 1912, with A Princess of Mars. That became a common part of science fiction, though it faded in the mid 20th cy. as it became apparent that the other Solar-System planets were not very nice places to live. Habitability defenders could point out that it was hard to know for sure unless one visited them, but when spacecraft did so, they reported back very hostile conditions.
So one had to have one's adventures outside the Solar System, and that's where Star Trek is set.