It still boggles my mind, the number of people who believe there is no difference between these two genres, but if there was no difference, why have they been given different names? It is true that they are similar, and that is why they are sub genres of “speculative fiction”, but they have differences which allow books to be categorized as one genre or the other.
Both science fiction and fantasy can be set in worlds past, present or future, which contain fantastical elements which make them different from the world we know. Both may contain people or objects with powers not found in our world. The main difference between fantasy and science fiction is how these worlds and powers are explained.
If a person’s powers are due to magic – its fantasy.
If a person’s powers are due to science – its science fiction.
In some worlds there will be a mixture of science and magic, but the dominant force will determine the genre.
If a spaceship flies because of magic – its fantasy. Just being set in space does not make it science fiction.
Luckily, most books in these genres makes it very clear to which genre they belong. Some ride the line between science fiction and fantasy, but ultimately they are defined as either sci/fi or fantasy never as both genres.
This is a pretty quick explanation and hopefully it makes sense.