The way I learned it was using the alphabet. We learned to associate A with being in the very middle of the bar, and then each line and space upwards is a subsequent letter in the alphabet. It took me maybe a couple of months to learn to tell what a note is at a glance, it's all in the alphabet. Once I had my ABCs properly worked in, then came practicing "moving" them up and down as needed, and that's some serious mental gymnastics! But I managed. Then I focused on learning the sharps & the bs, which came pretty easily to me as by then I was already very familiar with the position of each note. All I had to do was remember that sharps (#) are a half-tone up, & bs are a half-tone down. And that E# is the same as hitting F on the piano, Fb = same as hitting E, B# = C tangent, Cb = B tangent.
All in all, I think it just comes down to a combination of normal memory & muscle memory. It's really a lot easier than it sounds, it just takes a lot of practice. I'm sure others may have different ways of learning it too, that may be even easier.
Absolutely best way though, like Tica mentions below me (I'm tired x3), is to learn it from an instructor.