How do you know what notes to play in a certain song? . . . well, you learn the modes, and once you know all the modes you'll be able to move those patterns to whatever key you're in and play.
An alternate method of doing this is to learn the circle of fifths and the number of flats and sharps in each key. This is just another way to think of moving the modal patterns around the fret board.