If your trying to write something like a fugue, try looking into and studying some voice-leading concepts and counterpoint. Knowing more about those two things will help you understand what you might be trying to do.
The key to changing the music (in a drastic way) is to keep a small idea from the original idea and keep it going with a harmony over that part work itself towards the change. If the original idea is stable throughout, returning to its original form will be strong enough not to deceive the listener.
Motives are the key to this stability. Motives are small musical nodes, or the atom of a musical piece. They are usually only a couple of notes long, check the internet for a more descriptive definition and explanation. Motives then in turn can be changed and adapted throughout the piece, however they still keep the foundation that remains throughout the song. Keeping everything glued together. Check out Beethoven's 5th Symphony, 1st Movement to hear the most common rhythmic motive. Listen to how the rhythm stays static while the notes and direction of the music changes.
One way to change keys melodically is through a leading tone. The leading tone is the tone a half-step lower than the root of a scale. Ex. C major, the leading tone is B. However any note in a scale can be considered a leading tone, however for a different key. Ex. The Note 'E' exists within C major, but 'E' can also be considered the leading tone to move to the key of F. So through E you can go from C major to F (maj. or min.).
Chord Substitution is used best when your trying to harmonize a melody. Say the melody note is E (in the key of C major), and you want to play a chord to go with that melody note. Well the best place to start would be to use the chord that matches the melody note, which would be Em. But instead of using a chord that matches, you want to use something different. Well any chord with E in it will work. Let's just say you want to go with a C major chord instead. That's chord substitution.
"My whole life is a dark room...ONE BIG DARK ROOM" - a.f.i.