The quick answer is that it modulates to the relative major and uses secondary dominants. I would analyze m. 1-5 in G minor, m. 6 in Bb, and m. 7-8 in G minor. I added some notes of what I think are the highlights. I ignored inversions.
The first four bars are in G minor with a cadence on the dominant in bar 5.
The C minor on the first beat of bar 6 is a iv chord but acts as a ii in the relative major, Bb. I would analyze bar 6 in Bb (ii - V - I - IV) on the beats. He uses a pattern in the bass of root and shifting up a 3rd or down a 6th on the offbeats. The bass and tenor are resolving the tritone giving your cadence to Bb on beat 3 then moving to a IV on beat 4.
Bar 7 is the start of bringing it back to G minor. The A is a secondary dominant, V7/V, to the V, then to the unexpected G major and the offbeat F and Ab which make it a V7/iv (G7b9) resolving to C minor and a ii - V - i in the last measure.