When singing, you do indeed "hear" the music inside your head. But when learning to sing, just like when you're learning to draw, this isn't a fixed sequence: you try, listen, correct, and try again. A tuned piano nearby can help a lot to check whether you hit the note correctly.
If you're learning to sing from sheet music a melody you've never heard before there are basically three approaches:
Some people have "perfect pitch". They hear a note, and just know it's an A. That works two ways: they can also read the note, and try to sing it (if their voice isn't trained, they can at least tell they're wrong).
However, most people don't have this "gift". For those who don't have this, there are basically two approaches:
- Every interval between two notes can be thought of as an interval relative between these two notes. Basically, you try to remember those intervals based on intervals you already know from songs (for example, a minor third is the distance the Star Spangled Banner starts with, while most national anthems start with a perfect quarter)
- The other way is the "do re mi" way of singing, where the "do" is always the root note. When using this way, you try NOT to think of intervals as relative between the two notes, but always related to the root note.
The first way is somewhat easier to start with, as you need only to think of the interval between two notes. The second way is easier if you already have some experience with playing music. The second way also really helps to think/feel/hear "inside the key" which you're singing.
This works the same when transcribing a melody you've just heard: you repeat (sing) the melody you heard, and try to analyze what notes are in between. Either by determining all the intervals between notes, or by trying to relate everything to the root note.