(Apologies in advance if the following is something everyone already knows.)
There is a well-known saying in fiction writing: "Write what you know". Taken literally, this statement is misleading--as Stephen King once pointed out in an autobiography/how-to-write-stuff book, this would seem to indicate that one cannot write things like science fiction or fantasy, since, obviously, none of us have directly experienced such a thing. Rather, I prefer to interpret the saying in a different way: when writing a given story, make sure you know at least as much about the setting details as your target audience. This is important because most people find it distracting when their knowledge of how a given thing works conflicts with how that thing works in the story.
For example, let's say you're trying to write a teen drama set in a contemporary, American economically middle-class high school. If your target audience is ~18-40 year old middle-class Americans, the vast majority of that audience will have gone through high school. Not every high school experience will be the exact same, but nonetheless most of that audience will remember little details from high school, like that hot girl/guy in X class, the school bully, the nerdy/jerkass/nice teachers, the straight-laced school principal, the rowdy lunch room, the school gossip about who's dating who, etc. Because of the nature of these details, f you yourself are not intimately familiar with American high schools (i.e. went through it yourself, or at least studied it very extensively), then replicating the general high school experience from scratch will be extremely difficult. And if you fail to properly replicate the general high school experience in your teen drama, your target audience of 18-40 year olds will be too distracted going, "Wait, this is supposed to be a high school? My high school wasn't like this at all!" to pay attention to the rest of your story.
But keep in mind that you only have to know at least as much about the setting elements in your story as your target audience does. This is why Hollywood movies are able to get away with blatant historical inaccuracies--aside from the very broad strokes, most people don't know enough about history to be distracted when the inaccuracies come up. You might not be able to get away with the Nazis winning WWII in a historical movie, for example (unless it's very clearly stated to be a "what-if" story or something similar), but you could easily get away with generic German soldiers running around with MP44s (even if these were comparatively rare), the Wehrmacht using the wrong types of tanks at the wrong time, bizarre military strategies and tactics, etc. since most people don't know enough about WWII military hardware and such to really notice. Likewise, no one really gets into action movie-style gunfights against hordes of faceless gun-wielding mooks, so most people don't notice that the hero has very bad trigger discipline, or is making literally impossible shots all the time, or somehow survives that giant explosion behind him just be leaping dramatically at the right moment.
The implication of this is that speculative fiction--namely Science Fiction and Fantasy--are doubly able to get away with what would otherwise be oddities in the setting. In your generic Fantasy setting, for example, mages are often in service to non-mage kings. Depending on how magic works in the setting, however, it may make much more sense for mages to be kings instead. Nonetheless, few audience members will be distracted by something like this because, obviously, none of them have lived in such a world. No one has a real frame of reference to compare that Fantasy setting to, since no one has lived in a world with mages in it.
So...yeah. It's not so much "write what you know" as much as "make sure you know at least as much as your audience knows about the things you're writing", I think.