Setting is by far one of the most important parts of storytelling. Think how many stories begin with "In a faraway kingdom..." or "In a galaxy far, far away..." Location, or setting, not only helps define genre ('the Wild West' informs the Western, while noir is often set in grimy or shadowy urban landscapes), it also gives us a sense as to why things happen the way that they do - The Thing just wouldn't work outside of the Arctic, and nor would Twister be even remotely plausible if it was set in the Home Counties of England. Beyond that, the setting can almost become a character in itself - Mordor is a physical manifestation of the otherwise absent Sauron, while the island and its moods in Lord of the Flies reflects the transformation of the boys.
So how do you go about writing a good setting, or choosing a location?
If you're writing fantasy, you essentially have carte blanche to write whatever you want. Alice in Wonderland would be a perfect example! Science fiction in space is open to almost boundless possibilities, and even science fiction on Earth can be bent whichever way you want. Futuristic settings, or alternate realities, let you go crazy with the invention. I'd recommend Jasper Fforde's Thursday Next books for a good example of alternate realities. Swords'n'sorcery-style fantasy requires the kind of geography associated with the likes of Lord of the Rings - think castles, forests, plains, etc. Fairly generic, but as you don't need to have visited, you get to decide what goes where.
Of course, if you're writing the kind of fantasy wherein weird stuff happens to ordinary people, then you'll want to ground your story in a more realistic setting. After all, the weird happening becomes all the more weird when set against a mundane background. In this case, you'll need more of a grasp of where your story is taking place. You can set it in your hometown and just change the names, or you can keep the setting intact. It helps to keep things believeable - one of my many problems with 28 Weeks Later was how wantonly they screwed with London geography. Two of the characters are supposed to get to Wembley from Westminster via the tube tunnels, despite the fact that they'd need to change lines on the way! Once you annoy someone in that way, it's difficult to persuade them to further invest in your story. You've broken the 'suspension of disbelief'. These issues equally apply to other genres outside of fantasy.
But what if you want to set your story somewhere that you've never visited? Joanna Penn deals with the idea of how to write about a real location if you haven't been there in more detail, and I highly recommend that you read her post (the suggestion about using Google Maps or Street View is a brilliant idea). I actually recommend that you subscribe to her blog anyway as her posts are fantastically useful (you can also follow her on Twitter). Of course, you could always go down the Neil Gaiman route, and give your location the Neverwhere treatment - translate place names into their literal meanings (if you haven't read it, I highly recommend it as a masterclass on location). It doesn't matter if you've never been as the places are given a whole new meaning by you.
Of course, you could always treat yourself to a holiday and visit that pretty Alpine town you want to use as a backdrop to a 1920s murder mystery...