The simplest answer is practicing but there is a couple of different way to look at this. In the long run, the key is just to get used to being able to hear notes. I can pick up a E or an A for instance, pretty easily. You just get that skill over time.
That said, you might have a melody and wat to translate from your head to your hands. Sometimes, not as easy. You just have to hit notes until it sounds right. It's as simple as that. However, in doing so, are you going up a half step or a whole step? Get used to hearing those intervals. Also, often a pattern or song section, even of your own making, follows certain 'rules'. Once you determine the opening note of the riff or melody, does it follow a scale? Quite often, your brain knows the riff you just thought up, falls in to a common musical structure, riff, interval. Rare is the time that it wouldn't. Western music tends to fall in to those kinds of buckets.
It's not a magical answer but the knowledge you have now can help you convert what's in your head to your hands.
I hope that makes sense.