How do you form a D Major 7th in the key of C? Should I use the scale of D or C? Could you give me some tips on how to fit arpeggios and chords in certain keys?
Keys are descriptive, not prescriptive. This means that they help the person reading/interpreting the music to understand it. Also, the key does not absolutely force hard rules on the composer — it provides a toolset to work with, and some mental shortcuts; guidelines, basically.
In the key of C, something like a D Major 7th would be fairly rare since it uses two notes that are not found in that key. That makes it unusual, but not prohibited. Most importantly for this question, it does not change what a DMaj7 is. A specific named chord like DMaj7 or E minor or A sus2 is defined exactly the same way no matter whether you're playing it by itself, in a key where it "makes sense", in a key where it's unexpected, or even in a key/place in the music where it clashes and is horribly dissonant.
So, when it comes to selecting a chord/arpeggio/other element when composing a piece, you'll usually want to choose one that is built upon the notes found in the key, or a related key, etc. But when it comes to playing a specific named element, you do so based on the notes (or intervals, as Todd says) that said element is defined to be.
Matthew Read posted a fantastic (and concise) answer and everything he said is spot on.
I am posting this answer to provide a deeper understanding of the concept of building chords out of notes found in a given key. I hope this will help make sense out of chord theory as it relates to a songs key and why certain chords are commonly used in certain keys and how those chords relate to the key.
I will also go deeper into an explanation of how a particular chord is related to the key and the scale that corresponds to the fundamental (root) note of the chord - regardless of the key of the song where the chord is used.
TLDR - skip to the last full paragraph.
If a song is in a certain key, there is a musical "scale" that corresponds to that key. For example, if we are looking at a song in the key of C major, the corresponding scale would be the C major scale. This does not mean that the song can't contain notes outside the corresponding scale, but if notes are used that are not in the corresponding scale, we must use "accidentals" (sharp or flat or natural symbol next to the note on the staff) to indicate the deviation from the key signature.
The chords commonly chosen for most songs will also be derived from the corresponding scale - in two distinct and important ways. Let's examine those two ways below.
First - the notes in the scale (for the key we are using) will form the basis for the fundamental note (root note) of the chords that commonly correspond to that key. So for example in the key of C major, the chords will have as their fundamental or root - the notes C D E F G A and B (all natural notes). If we were in the key of C minor - the fundamental note or root note of the chords for the key of C minor would be C D Eb F G Ab Bb.
The above paragraph tells us how the common chord set for a given key is derived. But we need to look next at the notes in those chords - which will determine the "quality" of each chord (major, minor, diminished etc.).
So Second - the notes in the scale (for the key we are using) will tell us which notes will be used in the chords - which will in turn determine whether the chord is a major, minor, diminished or other type of chord.
Using the key of C major as an example, if we start with the first note in the C major scale which is C and use that as the fundamental (root) for our first chord (a/k/a I chord) - we will use C as the root and form the triad using notes available from the C major scale. Forming our chord from the 1st, 3rd and 5th degree of the scale we end up with C E and G which is a C major chord. Next we go to the second note in the C Major scale which is D. If we form a chord using D as the fundamental of the chord (root note) and we use notes from the C major scale - we will start with D as the root. If we choose the other two notes to form a triad based on D - from the C major scale, we end up with D F and A which is a D minor chord. Why you may wonder, is it a D minor chord and not a D major chord.
At this point we need to circle back and look at how a chord and the quality of that chord, relates to the root note of the chord (as opposed to the key the chord appears in). When examining a chord to determine it's quality (major, minor, diminished etc.) we can start with the major scale of the root note of the chord. So if we use a chord with a root note of C - the other notes in the chord and their relation to the C Major scale - will tell us what kind of C chord we have.
If we have the notes C E and G - we know that this is a C major chord - and it's a C major chord regardless of the key of the song it appears in. We know it's a C major chord because we know that a major triad is formed from the 1st, 3rd and 5th degrees of the major scale that corresponds to the root note (in this case the C major scale). If the notes in our particular C based chord (C is the root) happen to be C Eb and G - then we know this is a C minor chord. We know it's a C minor chord because of the interval between the 1st and 3rd degree of the scale.
In simple terms, a minor chord is one that uses a "flatted" (lowered by one half step) 3rd degree of the major scale of the root note of the chord. So we know that a C major is formed by the 1st (C) 3rd (E) and 5th (G) degree of the C major scale. Then if we flatten the 3rd degree from E natural (in the key of C major) to E flat (flattened 3rd and not in the key of C major), then we have a C minor chord.
To expand this explanation to the D chord mentioned above as our ii chord in the key of C major - a D major chord is based on the D major scale (regardless of what key song it is used in). Just like any major chord including C major, a D major chord is formed from the 1st, 3rd and 5th degree or note of the D major scale. The notes of the D major scale are D, E, F♯, G, A, B, and C♯. Thus the 3rd degree of the D major scale is F# so a triad formed with D - F# - A is a major triad or D major chord.
Now let's go back to the concept of using the notes found in the key of the song to form the chords used in that song. If we are looking at our ii chord in the key of C major - the root note for our ii chord is D (2nd note in C major scale). I mentioned earlier that if we form a triad based on D - and choose our notes from the C major scale, we end up with D F and A which is a D minor chord - because F (from our C major scale) is the "flatted" third degree of the D major scale (scale based on the root of the chord itself). This makes it a D minor chord by our simplistic easy to understand definition of minor chord (flatted 3rd degree of MAJOR scale corresponding to root of chord).
As we continue through the notes of the C major scale to choose other chords based on these notes and we apply the same principle of forming the chords from notes in the C major scale, we end up with the chords C Major (I) D minor (ii) E minor (iii) F Major (IV) G Major (V) A minor (vi) and B diminished. Okay - "so why is the chord formed from using the 7th note of the scale as the root diminished?" - you may be wondering.
In the key of C major, the 7th note in the C major scale is B. If we want to form a chord using B as the fundamental (root) and using only notes from the C major scale - we come up with B - D - F. So what kind of B chord would that be (no pun intended)?
Going back to what I said earlier about using the major scale of the root of a chord to determine it's quality - we can start with a B major scale. This gives us the notes B, C♯, D♯, E, F♯, G♯, and A♯. We know a B Major chord would be comprised of the 1st, 3rd and 5th scale degree or B - D# - F#. A B minor chord would flat the 3rd degree D# to D natural and contain the notes B - D - F#. But since F# is NOT in the key of C major and we want a B chord that is also in the key of C major, our choice of notes is B, D and F. So now we not only have a flatted 3rd degree but also a flatted 5th degree or a minor chord with a flatted 5th - which is called a "diminished" chord.
Using the same process of forming chords using the notes from the scale corresponding to the key of the song as the root note (fundamental note) while building the rest of each chord using notes from within that scale, we can get many other chords such as 7ths, augmented, minor 7ths, major 7ths, 9th chords and so on. But the one thing they will all have in common is that the root and the notes of the chord will be derived from the scale that corresponds to the key. The quality of the chord (major/minor/diminished/etc.) will be determined by how those notes relate to the major scale corresponding to the root note of each chord - not the scale corresponding to the key of the song.
If that sounds confusing, re read the examples above.