The D chord is acting like the V of the Gm chord, and is followed directly by the Gm chord.
The F# in the D chord is acting like a leading tone into the G of the Gm, essentially "tonicizing" the Gm chord and making it a temporary tonic chord before moving on in the progression.
music.stackexchange.com/users/47856/Heather S.
The D-g-C7-F is just a ii=
V7-I ending preceded by the (local) dominant of the ii chord.
The analysis is often given by V/ii-ii-V7-I.
(Or is some books, VI-ii-V7-I.)
Any major or minor chord may be preceded by its own dominant (or a few other chords) without changing the essence of the progression.
The fundamental bass (D-G-C-F) falls by fifths so sounds good.
With a slight change, one could have vi-ii-V7-I which is common from at least the Baroque era to today.
The two progressions have different sound (or color) but are basically the same harmony.
music.stackexchange.com/users/23838/ttw
It has been said that the D is acting as a 5 chord to the Gmin.
This is a common device.
D is the relative minor to Fmaj (clearly the key).
Tunes will often put a dominant seventh chord on the vi in a progression.
Case in point, any Bebop Rhythm Changes: Anthropology, Oleo, etc.
Cycle extensions work this way by treating a chord in the progression as a temporary I and placing its V7 or a ii-V before it to create resolution.
In Jazz this is sometimes overdone, filling up measure after measure with 4 chords.
The effect can be interesting.
music.stackexchange.com/users/50691/ggcg
It's only 'theory', but as Heather states, it's the dominant of Gm (V/ii).
Another theory states that chords can be 'borrowed' from parallel keys .
It's a little convoluted, but key F has Dm as its relative key, which has a parallel key of D major.
Thus it fits!
And cycles of fourths/fifths have been used for centuries.
Usually getting back, eventually, to the root.
How far away from that root the 'modulation' starts varies, but this sequence is quite short compared with others.
As Laurence says ( and rightly keeps saying)
we spend maybe too much time trying to justify everything.
Maybe that's just human nature, but in this case, the sequence works, like it does in hundreds of other songs, so by now, it's simply de facto.
music.stackexchange.com/users/2808/Tim
It used to be a very common progression in popular music, a "circle of fifths" starting from as far away from the tonic as you like, and working back to it.
In F, the longest sequence would be F E A
D G C F. Add 7th's, or change some of the chords to minor, as you like, but the first chord after the tonic is almost always major or a (dominant) 7th.
The chord after the F (whether E, A, or D) comes as a (slight) surprise, because of the major 3rd of the chord (and the 5th of the E chord) are not in the scale of F, but the sequence of dominant-tonic resolutions gets it back home safely.
You can find lots of examples "before rock" -
George Formby used it in many songs, for example.
And in classical music, it goes back at least as far as Mozart.
In the baroque era, similar progressions using minor chords or minor 7ths, staying within the notes of the key, were very common.
music.stackexchange.com/users/None/
"It is neither a diatonic chord in F major nor F minor."
And why should it be?
There's no requirement or virtue in using only diatonic chords.
I'm slightly surprised it's plain D,
Gm... rather than D7, Gm7...
But it's still a standard 'cycle of 5ths' turn-around.
Add it to your musical tool-box!
music.stackexchange.com/users/17003/Laurence Payne
I am taking a music theory online course and there is an example I don't understand. The chord progression is:  F Bb C F D Gm C7 FMaj7  The first 4 bar is just I-IV-V-I and the last three bar is II-V-I. Question is what brings D here?? It is neither a diatonic chord in F major nor F minor.
theory - D major in F major chord progression?
music.stackexchange.com
<theory><chords><chord-theory><chord-progressions>
music.stackexchange.com/questions/72577
music.stackexchange.com/users/51538/ddqq
The D chord is acting like the V of the Gm chord, and is followed directly by the Gm chord. The F# in the D chord is acting like a leading tone into the G of the Gm, essentially "tonicizing" the Gm chord and making it a temporary tonic chord before moving on in the progression. The D chord is acting like the V of the Gm chord, 
 The D chord is acting like the V of the Gm chord, and is followed directly by the Gm chord.