Often when a story has elements of both science fiction and fantasy present, one genre will be more present than the other. This allows the story to be classified as only that genre with a hint of the other. But sometimes, both or neither side will want a subgenre or a story.
And then things get fun.
My favorite example of this currently is the steampunk subgenre. It should seem like a very cut-and-dry classification. Steampunk explores how technology might have developed if we had moved toward hydroelectrics instead. At its core, it’s a heavy study of science, technology, and engineering, making it fall squarely into science fiction (where it originally appeared as a subgenre). But then people attracted to the Victorian settings steampunk stories often take place in took the subgenre in another direction, exploring darker aspects of history and drawing in supernatural creatures, tugging steampunk toward fantasy.
Now when you ask whether steampunk is a subgenre of science fiction or fantasy, you get a very interesting (and often very spirited) answer because both sides really claim elements of the subgenre.
On the other side of the coin, space opera Star Wars has become a small battlefield. Long regarded as one of the landmark science fiction franchises, the entire franchise is now facing a possible reclassification. More hardcore science fiction fans can’t figure out where the science is (because the Force and its midichlorians are presented as a magical power), which means Star Wars fails the most basic definition of being a science fiction story. But no one can really wrap their mind around a fantasy story set so entirely in an alien space. Now when these fans talk about rejecting Star Wars from science fiction, they start talking about “future fantasy”, a subgenre that has yet to really take root.
Some of us love both enough that we’ll enjoy well-executed efforts at combining the two, but it’s an interesting little minefield at times.