When writing sci-fi or fantasy, at least a basic element of world-building is needed. ‘World’ can refer to a planet, or the entire galaxy, or a single city, and just means the immediate and general environment that the story takes place in.
In both sci-fi and fantasy the author can choose to incorporate the real world into their story (like in Harry Potter and Star Trek) or not. The advantage of a real world link is that you’ve already got a basic template, but then you may have to either do some historical research or conjure up a reason why nobody notices magic happening.
Personally, I tend to go for entirely fictional worlds. There’s more freedom, if a bit more work, and you can still take advantage of real world information (like adapting a feudal system or using medieval fashion to dress the people milling about in Fantasyland).
For those interested, there’s a brilliant link below which offers a lot more info about generating numbers for a medieval kingdom:
A bane or blessing with world-building is that the level of detail is entirely dependent upon how much work you’re willing and able to put in. There’s nothing wrong with having a basically 13th century English country, with pig farmers and yeomanry and longbows. But, you could introduce a new element. Different intelligent species, or the practice of necromancy, or flightless birds that can be ridden like horses (such as ostriches or chocobos).
As well as specific details, there is the general feel of a world to consider. Two common sci-fi types are the ideal world (such as in Star Trek where humanity is lovely and nobody dies of starvation) and dystopian (the bonkers Aussie series Farscape is a good example, where there’s a war between the evil, imperialist Peacekeepers and the even more evil, psychopathic Scarrans).
In fantasy, there’s been a distinct shift from the relatively straightforward good versus evil of Tolkien and Lewis to a greyer, more morally ambiguous worldview (excellent examples include Joe Abercrombie’s First Law Trilogy and George RR Martin’s A Song of Ice and Fire). Being a cynical sort, I generally prefer moral ambiguity.
One of the key differences between the two genres is that (typically) fantasy has magic and sci-fi has advanced technology, and these alter the way the world works. Crucial to sci-fi is faster than light travel, due to the enormous distances in space. But, this could be limited to a single organisation or species, or the fuel could be controlled by a cartel. Or the universe could be your oyster, with every man and his dog able to go faster than light.
Often, a more detailed and unusual world can really draw a reader in. It also helps develop the context of dramatic happenings and can flesh out characters and explain their vices and virtues.