The BBC science fiction series Doctor Who may at first glance be the obvious answer. It aired its first episode, An Unearthly Child, on 23 November 1963 and its 851st, Resolution, on 1 January 2019. In that fifty-six-year period, Doctor Who has produced 37 seasons of television, with the 38th season due to air in 2020. Approximately 413.3 hours of material has been produced, not including the spin-off shows (Torchwood, The Sarah-Jane Adventures and Class) in that time. This is more than twice as much material as has been produced by the likes of Stargate SG-1 and The X-Files, which have both erroneously been claimed to be the longest-running SF show in the past.
So, in terms of both length and quantity of material produced, Doctor Who is the clear front-runner.
However, some may dispute the position on several grounds. Doctor Who spent seven years off-air between 1989 and 1996, when a 90-minute special was produced, and then another nine years off-air between 1996 and the rebooting of the show in 2005, so it has not been in continuous production for all that time. Furthermore, when Doctor Who returned in 2005 it was officially designated and treated as a new show: the season count was reset to 1, so the most recent season to air is counted by the BBC as Series 11, not Season 37. This is counter-disputed because of legalities and technicalities: reprising Doctor Who in 2005 using the same framework as in place in 1989 was impossible, so the show had to be counted as a new series for technical purposes, but in terms of title, characters and continuity it is clearly meant to be a continuation of the 1963-89/96 series.
Despite that, it is certainly true that Doctor Who is not automatically the longest-running, continuously airing science fiction or fantasy TV series in the world. That honour is claimed by several other shows.
Paranormal adventure series Supernatural began airing in September 2005 and will conclude in 2020 with its sixteenth season. By the end of that season, it will have aired 327 episodes with a combined run-time of 239 hours and 48 minutes. This eclipses Doctor Who’s tally of 155 continuously-produced episodes since March 2005, totalling 124 hours and 20 minutes, almost only half of Supernatural’s run-time.
A wild card to throw into the mix is the web series Red vs. Blue, which began airing on 1 April 2003 and aired its most recent episode on 25 May 2019. It has totalled 364 episodes and 17 seasons released to date. However, Red vs. Blue’s episodes only average about 7 minutes in length, with the total length of the entire series to date weighing in at a relatively modest 41 hours, 57 minutes.
So the answer to this question is as follows:
Doctor Who is the longest-running SF series in the world in terms of total number of episodes produced, hours aired and seasons made, including hiatuses.
Supernatural is the longest-running fantasy series in the world in terms of total number of episodes produced, hours aired and seasons made. It is also the longest-running SF or fantasy series in the world in terms of the continuous number of episodes made.
Red vs. Blue is the longest-running, continuously-produced SF or fantasy series in the world in terms of dates, predating Doctor Who by two years and Supernatural by two-and-a-half years, but it has produced significantly less content than either.
Note: for the purposes of this article I have considered only live-action shows. Animation would be a different and potentially more difficult question.
Again not counting animation, the longest-running, completed SFF series is Dark Shadows, a supernatural soap opera. Although it only ran on American TV for six seasons between 1966 and 1971, it amassed an enormous 1,225 episodes in that time, for a total run-time of 612 hours and 30 minutes.
This has a more straightforward answer. The longest-running science fiction or fantasy franchise, spanning multiple shows, is Star Trek. It aired its first episode, The Man Trap from Star Trek, on 8 September 1966 and its most recent, Such Sweet Sorrow, Part II from Star Trek: Discovery, on 18 April 2019.
On television (not counting the movies), Star Trek has spanned seven distinct series: the original (1966-69), The Animated Series (1973-74), The Next Generation (1987-94), Deep Space Nine (1993-99), Voyager (1995-2001), Enterprise (2001-05) and Discovery and its related Short Treks spin-off (2017-present). It will soon be joined by Picard, Lower Decks and Section 31.
In that time the franchise has amassed 762 episodes in total, amassing a grand run-time of 555 hours and 21 minutes.