Write what you know is the advice most often given to new or would-be writers, but where would Jules Verne be now if he had followed it? You can certainly "know" what you can imagine.
Still, "write what you know" can be good advice. For instance, I think a lot of writers draw on their real life experiences. Dick Francis grew up with horses and was a professional jockey for a long time. Most of his books contain either horses, or jockeys, or both and that’s no surprise. But it goes much deeper than that. One of the first was about a jockey who lost the use of his hand (Francis suffered a hand injury). One is about a pilot trying to make it back across the English channel in a damaged plane (Francis was a bomber pilot in WWII), another about a writer who cares for his wife, a victim of polio (Francis’s wife had polio), one about a famous man who inherits a gold mine (Francis is famous and owns or did own a gold mine), one about an artist who paints in acrylics (Francis paints in acrylics, an interest he developed as therapy for the injured hand), and two about a jockey who rides for a very classy middle-aged princess (Francis once rode for the Queen Mother).
Still, you can also know anything you can research.
After I saw the movie CROSS CREEK I set a story in Florida, though I had never been there at that time, and I worried that I hadn’t gotten the "Florida" atmosphere right. Although, some people had told me, “a marsh is a marsh is a marsh” even when they call it a “prairie” as they do in Florida. I felt unsure. I live near a marsh with muskrats and mosquitoes, so I do know marshes.
I wrote a “coming of age” story in which a young girl loses her virginity, and used an alligator hunt as the metaphor for that. I've probably mentioned this story before, because it was one of my most spectacular failures. I put in lots of what I thought was sexual symbolism and tension between the girl and the older man, a friend of her father’s, who had just been waiting for the opportunity to take advantage of her, etc. But I was worried about the setting, so I read it in a critiquing session with about 17 women, five of whom I knew were from Florida. I felt sure they’d pick up on any bloopers. Well, the form for critiques at was that you read your work aloud, and then shut up and listened. People in the circle answered three questions put by the moderator:
How did you feel while the story was being read?
What would you change if you were writing the story?
Only after everyone had answered those questions, could the author ask for details, like, “Well, was the setting accurate?”
The answer to the first question stunned me. “This girl and her brother went alligator hunting.” Whoops! If they thought he was her brother, then they didn’t get my sexual content at all. My job was to give it to them and I had failed miserably. Nobody had to say "it sucked!" That isn’t relevant to begin with. Nobody had felt bored, which I believed was luck rather than skill, since nobody had got the plot. When it came my time to ask, the Florida people said they had assumed I lived there, too, as the detail was so accurate. Most of those place images had come straight out of the CROSS CREEK movie, which had been gorgeously filmed in Florida.