By spelling, I mean the notes it takes to make a chord. CEG for C, GBD for G....
by changing them you create a sound that seems to be 'moving' away from, or toward, the tonic.
Key of C
C is the Tonic.
CEG = C chord
CEG are all in the key of C
The D chord is made of D F# A. The F# is not part of the key of C and so creates an unstable feeling/sound that seems to move the music in some direction.