The origins of science fiction are not very clear. Probably because there is a dispute as to what it has to include. At its heart science fiction has to have an element of science to it. It is fiction based on scientific theory or extrapolation. It can either delve deeply into the science or it can push the science to the background and make use of science in a more general sense. If we can determine how much science a story needs at present to be considered science fiction, we can reach back into history to find stories that rely on science to a similar extent and those would be the earliest science fiction stories.
There is competition between hard science fiction and soft science fiction. Many maintain the standards of hard science fiction to be the true standards of real science fiction. In this type of story, there is no extrapolation of what science might lead to in the future, only what modern science has proven and what technology based on that might be. There is no attempt to include theoretical science, though theoretical technology is allowed. This is all well and good, but historically, this is only a very limited slice of the greater genre. Historically (and we only have to look to Jules Verne and H.G. Wells, often cited as the first writers of modern science fiction) that has not been the case. Wells in particular imagined time machines, alien life forms, advanced technology well beyond any science of the day, and much more. So from the outset, science fiction has let in a bit of fantastical imagination. As long as it is grounded in science. The golden age of science fiction was full of fictional advances in human understanding and technology. Stories took us to the moon in scientifically plausible vehicles, traveled in time, took us to far away worlds with strange aliens, brought us alien monsters, robots that can pass as human, humans with amazing powers, imaginings of zero gravity generations before the first real traveler in space, and so many more things that were not then or still are not a reality.
And we must draw a clear line between what is science fiction and what is fantasy. Fantasy is not any fantastical story. Fantasy is grounded in magic. The magic may follow rules, but there is no attempt to ground the magic to reality in any way. Arthur C. Clark postulated that a sufficiently advanced society would appear magical to a sufficiently less advanced society. But that is not what fantasy is about. Fantasy is magic. There is no advanced society. It is magic used by members of the society. There may be attempts to make the magic behave in a logical and orderly manner, maybe even a quasi scientific explanation to the magic, but ultimately it is magic and not science.
A few bend the borders between these. Terry Brooks and Mark Lawrence both wrote fantasy based in a future world. Robert Jordan’s Wheel of Time series is set in a world where science and magic have worked together in the past. But it is clear with the way these stories are written that the focus is magic and any science is in support of the magic.
George Lucas muddied the waters by calling Star Wars science fantasy. He also correctly labeled it space opera. Like the paragraph above, the Force in Star Wars, and the occasional tropes borrowed from fantasy, are used in support of the science based, futuristic setting. Star Wars is a direct descendant of the planetary romance genre of the early 20th century. The Force is just an offshoot of the ESP based mental powers so often used in early science fiction literature. The light saber is a sword based on advanced technology that can cut anything. Its very setting swirls with technology. Robots are some of the main characters. The story is taken from myths and legends much like a lot of early science fiction of the golden age. The adventures of Northwest Smith make the adventures of Luke and Han seem tame. Star Wars is based on the mythic hero’s journey and so many early science fiction stories are based on Greek mythology. The problem with the parallel science fiction is that it is soft science fiction. When you listen to George Lucas talk about Star Wars, he is clearly using science fantasy to make a clear break from hard science fiction, not all science fiction.
With these difficulties and with both science fiction and fantasy (and to some extent horror), some group them together in a larger genre called speculative fiction. Most of the truly old stories that get cited as early science fiction are more properly speculative fiction. That is because the science is not clear. They are imagining the future, but it is not always based on technology or science, but just a general speculation. The Wikipedia page on the history of science fiction (https://en.wikipedia.org/wiki/History_of_science_fiction) is full of references that on further examination are more speculative fiction that science fiction. and the Wikipedia page on the history of fantasy (https://en.wikipedia.org/wiki/History_of_fantasy) makes it clear that the two genres follow distinct lines.
With the advent of some modern subgenres like steam punk, as well as examining older works, it becomes necessary to think of science fiction and fantasy in different terms. Science fiction is about the wonders of technology and science. Fantasy is about the wonders of magic and enchantments. So today you can have a science fiction story set in a medieval setting and a fantasy story set in the future. But it is clear from the lineage to which each belongs (unless the author is deliberately blurring the lines of the genres). And horror is clearly about one of the other of these two gone wrong. Frankenstein and the movie Alien are both horror more than science fiction, though both clearly are filled with the imagined wonders of science and technology. But both are more about the nightmare than the science. But all are speculative fiction. All come from different roots but have become so similar and intertwined in some ways that the lines are blurred and sometimes hard to distinguish.
Horror, like fantasy, is not based in reality. Where you could call fantasy a dream, horror is a nightmare. It tends to the occult, demons, murders, and such. Many of the seminal works came out of the 19th century. From Frankenstein to Dracula and so many other tales in between. In many ways the reason that we call the greater genre speculative fiction is that there are so many stories that cross boundaries or at least are clearly neighbors. Frankenstein is also considered science fiction. Dracula is also considered fantasy. The Invisible Man is science fiction and The Picture of Dorian Gray is fantasy. But only if you ignore the horror element.
In many ways all components of speculative fiction have always been historically intertwined. They all require that the writer let their imagination go and dream something into existence that does not exist. Science, legends, nightmares, they all have something to add. But the birth of science fiction is tied to science and reason and extrapolating what those could lead to. The first true science fiction writers were Jules Verne and H.G. Wells. There were writers who came before, but often the science is clouded. Looking to the future does not make a work science fiction. Traveling in space with aliens does not make a work science fiction. It must have that science component. It must have technology that is based on science. It may not delve very deep into science, but it must have that component. Cyrano de Bergerac writing about a voyage to the moon hints at the imaginings that would become science fiction. Frankenstein has our first mad scientist. But are these really science fiction? I challenge you to read them and see for yourself. I challenged myself to read more from the golden age some years ago and stumbled on Northwest Smith. In those stories I could see Han Solo and Captain Kirk. I could see the mix of adventure and scientific based speculation on the future. Mixed with a healthy dose of myths and legends.
I will not say anything is definitively not science fiction. In fact my chief argument is with those proponents of hard science fiction who refuse to acknowledge the wider history of science fiction and claim works that are softer on science and offer more speculation are fantasy rather than science fiction. That is most definitely a false claim. Those works are clearly science fiction. And historical works from before the mid 19th century are harder to categorize. It isn’t until the golden age that we can start to clearly see the genres we know today. Before that the names were different. Tolkien is almost singlehandedly the father of modern epic fantasy, even though there were quite a number of authors with similar tales going back a century before The Lord of the Rings was published. But while they laid the groundwork, their works are not as widely know. Shelly, Poe, Verne, Wells, and Tolkien are the names people will recognize. Speculative fiction would not be the same without them. Before that lies a history of works that led up to modern speculative fiction, but it was a growing period and classifying those older works is problematic. I feel that we should acknowledge the contributions of the past that led to each genre, but not worry too much about how well those old stories fit with the modern genres. They aren’t supposed to. They are what came before - the history and lineage. It is rich and complex and hard to categorize.
I will suggest that speculative fiction as we know it was born in the 19th century. The different varieties were born of different traditions. Their origins are different and they are different today, but they are distinguished from other forms of literature by their speculative nature. It just depends on whether your speculation is about science, magic, or nightmares.