So, I have been playing this chord progression in the key of D major with the chords DM7, Bm7, GM7, C9, and I can't seem to figure out why the c9 resolves so nicely to the D major 7 chord. Is it just a borrowed chord?
This resolution is called a backdoor cadence, which part of another common progression referred to as the backdoor ii-V, and it is used often in jazz standards.
This article from educator Anton Schwartz includes a list of jazz standards that utilize it: https://antonjazz.com/2012/01/backdoor-ii-v-progression/ It also includes some information on the theory behind it. Here is the relevant passage:
Why does the backdoor progression work?
There are many ways of looking at the backdoor progression, but here are a few. For simplicity, let’s talk in the key of C, making the progression Fm7 | B♭7 | C:
First, the B♭7 chord of the backdoor progression is very closely related to the G7 and D♭7 chords of the normal and tritone-sub ii-V progressions. If we voice each of the three chords with with a ♭9 and natural 13, they all use the same B diminished chord and whole-half diminished scale. And we may see the chord tones of the B♭7 as the ♯9 (B♭), 5 (D), 7 (F) and b9 (A♭) of G dominant.
The Fm7 chord of the backdoor progression functions much like the Dø7 chord, which leads to C in a minor ii-V progression. The Dø7 chord is effectively the relative minor of the Fm7 chord, since it is based a minor third down from the Fm7 and the 1-3-5 of the Fm7 are the 3-5-7 of Dø7. Differently put, an Fm6 chord is merely an inversion of a Dø7.
The backdoor progression leading to a major key is the tritone sub progression of the closely-related relative minor. Since Fm7–B♭7 leads easily to Am, it should be no surprise that it also leads to Am’s relative major, C.
The song Lady Bird by Tadd Dameron starts with a progression very similar to yours. Here are measures 1-5 (transposed to D major):
Dmaj7 | Dmaj7 | Gm7 | C7 | Dmaj7
Your progression uses a GM7 in bar 3, but the function of the C7 (or C9 in your case) is the same. Essentially, Lady Bird is in D major for two measures, then F major for two measures, then back to D major. Your progression is in D major for three measures, then F major for one measure, then back to D major.
Well, a rule of thumb is that a resolution is nice (and sounds as such) if you can go from the first chord to the second just by moving each tone up or down by at most a tone.
(So, if you put a bit of thought into it, you will see that it's actually not entirely easy to write two chords so that the first does not resolve into the second!)
So for instance, A7 resolves nicely into D major, because you send A → A, C# → D, E → F# or D, whatever, and G → F# or A.
Another rule of thumb would be that the resolution is "more agreeable" the shorter way the tones have to go. So for instance, it's good to have some notes that rise/fall by a semitone. The more, the better (usually).
That's the reason why we sometimes borrow out-of-key chords for a resolution. For instance, B♭ major resolves nicely into A minor, because B♭ → A is a semitone, F → E is a semitone and the D can go either way by a tone. So here you have two semitone steps instead of one.
Let's apply the rules of thumb to your case: C9 contains C, E, G, B♭ and D, while DM7 contains D, F♯, A, C♯. You can immediately see that there's a lot of potential for the semitone steps. An extreme case would be C → C♯, E → D, G → F♯, B♭ → A and D stays (but here you must pay attention to the voice leading, for instance if you have C in the bass, it would be bad to send it to C♯, because the major 7th in the bass sounds really unstable etc.)
The best way to see that is to just look at what shapes you play on your guitar. If you play a "Django-style" C9, your third finger will actually hold a G minor triad on the treble strings, which will then slide a semitone down into the F♯ minor triad that is a part of the DM7, while you send C → D and ignore the E. So in this way, you get a nice, "balanced" resolution, because the top three voices go down while the bass goes up.
And by that, you have also satisfied the third rule of thumb: don't move everything in one direction. Ideally, you should abide by the stronger, classical version of this rule of thumb: the bass should be moving in the opposite direction to the other voices. This rule is a couple hundred years old but it's still good. It just sounds nice.
There are a few reasons, the first and most important is good voice leading:
C-C# (or D)
E-F# (or D)
D-C# (or D, common tone)
Next, in its basic form of bVII (and for that matter as a 7 or 9 chord too), it is a chord borrowed from the parallel minor. The bVII is probably the most commonly used non-diatonic chord in many styles of music. It has been used in hundreds of songs and just works and sounds good. I have participated in several other answers regarding the bVII chord in this site, a simple search will give you some more info on it and its many uses.
One way to see why a more complex chord sounds good is to look at a simplification. Sometimes root motion is enough, and C->D is indeed a common resolution call the "backdoor" resolution, and is borrowed from minor.
But you can also look at the other parts of the C9. Without the root, it is an Edim7 chord. And if you remove the root of that, you have Gm. Those are the ii°7 and iv chords in D, respectively. Both of those are common borrowing from minor. By flatting the 6th scale degree in the major scale, it increases the pull to resolve to the 5th scale degree. The 4th scale degree also moves by half step to the 3rd. This resolution is so strong that some consider it the negative version of the dominant seventh chord.
Finally, you can just look at the top notes of the chords. The usual top note of C9 is D, and the usual top note of Dmaj7 is a C#. So, rather than hold on the D like you would based on the previous paragraph, you add a bit of extra motion that gets you to another jazzy sounding chord.
With two strong borrowings from minor and then a strong half-step resolution on top, it's no wonder it resolves so well.
If you build a triad off the fifth of the C9 (G Bb D), you get a G minor, which gives you a minor plagal cadence which as the link notes, resolves to the tonic.