Basically, we use the vast amount of recorded guitar music as a guide. We listen to the guitarists who have come before us and use their tones to help us to make educated guesses as to how to achieve the tones we want. A lot of the times, certain gear is closely identified with certain players and certain sounds. Here are some examples of pieces of gear and players closely associated with them (this is, obviously, very non-comprehensive; if you feel I've made an egregious omission, leave a comment):
- Fender Stratocaster (or Strat-like guitars): Jimi Hendrix, Eric Clapton, Jeff Beck, Richie Blackmore, David Gilmore, Mark Knopfler, Stevie Ray Vaughn
- Gibson Les Paul: Jimmy Page, Slash, Billy Gibbons, Ace Frehley
- Fender Telecaster: Keith Richards, Bruce Springsteen
- Marshall Amps: Hendrix, Slash, Angus Young
I could go on and on associating gear and players, but my point is, if I want a sound approximating that of, say, Slash, then I don't go looking to buy a Strat. I go buy a Les Paul and a Marshall JCM-800, and I'm 95% of the way there.
Even if I don't want Slash's tone exactly, I know what a Les Paul sounds like. I've heard dozens of players playing Les Pauls, and I've spent enough time in guitar stores just fiddling around on them, so if I pick up a Les Paul, I pretty much know what kinds of sounds I'm going to be able to get. And that works in reverse, too: if I have a particular sound in mind, I can guess with reasonable accuracy what kinds of guitars (and amps, and pedals, etc.) will be capable of producing that sound.
But look: for most of us, most of the time, if we get 95% of the way towards the tone we're looking for, that's good enough. Some players obsess over every last subtlety of tone: Eric Johnson, famously, claims he can tell the difference between the brands of batteries he uses in his effects pedals---but then he's got the time and the money to worry about such things. For me, I'm going to be playing in a club with bad acoustics, through an aging sound system with a house sound engineer who may or may not care about my tone, in front of a crowd who I hope is getting good and drunk. The circumstances of the environment completely wash out any subtle details in my tone. And so instead of focusing on getting exactly the right combination of gear and settings, I spend my time focusing on the quality of my performance.