This isn't an easy question to answer.
The answer goes back to basic music theory and which notes are in which keys. You have to have a working knowledge of the circle of fifths and all the modes.
Do a search at wholenote.com or search the forum here at Guitartricks for an explanation of these.