Science fiction is a subset of fantasy. Fantasy is any work in which the setting is deliberately made different from the real world. Science fiction is when those differences are due to the presence of advanced science and/or technology.
Counterpoint: Science fiction is fiction which shows a world different from our own, but different due to changes in culture or technology that plausibly could take place within our own universe, whereas the differences in fantasy are ones that are fundamentally incompatible with the known physical laws of our own universe.
Edit: How sharp of a division one wants to make out of that "plausibly could" clause is the dividing line between hard and soft scifi.
This is a great definition. There's a lot of "sci Fi" that is much more firmly in the realm of fantasy (I say this as someone who kinda likes Dr Who). Being in space is not enough IMO to be called sci Fi.
What about sci-fi that's literally only different from the real world because of fictional technology? That makes up a bulk of the kind of sci-fi I am into. Cyberpunk and the like.
Star Trek technically takes place in the actual universe. Is it fantasy because it's almost never on Earth?
The genre is usually divided into "soft" and "hard" fantasy.
Cyberpunk is generally considered hard fantasy, as is stuff like The Expanse or Interstellar.
Star Wars is unabashedly soft SciFi, it's a straight Fantasy story in space.
Star Trek is a half-breed - it pays some lip service to scientific "plausibility", but much of it stretches that envelope beyond the breaking point. Scientific accuracy was never the point of the series to begin with.