http://www.vai.com/LittleBlackDots/MLS_index.html
first of all, have a read through those articles. some of the most mind opening and informative stuff you'll ever read.
from my own experience, i think ear training and being able to identify intervals and chords really well is a large part of it. when i hear stuff in my own head, i "see" it in terms of intervals and scales and how i want it to sound etc.
i won't go into depth about what happens in my head musically, because i don't even understand it myself, it just works. i can't put it into words. but i think the key is linking your mind to your guitar through ear training. SOMETHING like that.