You can add whatever you want, but it might not sound the way you expect it to.
I assume that the effect you want is for it to sound "consonant" (ie no dissonances) so that it sounds pleasing to the ear following our Western popular music convention.
In that case, it's not just about the key: you also need to follow the same tempo and the same chord progression.
Following your D major example. If you add a track/melody that's playing on the notes of a D major chord while your song is playing an A7 chord, the tension effect might be broken. Conversely, if your new track is playing on an A7 chord when your song playing over a D major you might have a dissonant effect.
Also, the tempo. If the tempo of the new tracks are mis-aligned, you will get an unpleaseant "off-tempo" feeling.
Many accomplished musicians, especially coming from a jazz background, can easily improvise a new tune ontop of an existing chord progression. However, this takes either some good musical hearing or a fair amount of training.