A single bassline can be harmonized in a number of different ways. Assuming you are working only with diatonic triads (three note chords that require no accidentals), you'll typically have three options for your harmony for each note. In the key of G major, those options look like this:
I realize this probably looks a little daunting right now, but it becomes easier as you practice. To show you how we can apply this, let's look at the last four measures of your exercise. We have the following bassline: D, E, C, D, G, C, G. I usually find it easiest to work backwards when harmonizing, so let me walk you through that process:
First of all, we already know the last chord has to be a
I chords in root position, so let's look at the second to last chord. We know we need a cadence here, either plagal or dominant. The only chord we can use that gives us a valid cadence is a
IV, a C in root position. So, our last two chords are basically decided for us. From here on, we have to start making some decisions...
This is where we start to get out of the realm of right and wrong answers. You could, for instance, harmonize all six of these chords in root position and not break any rules (
I). That would probably sound alright, although not particularly creative; besides, we want learn about how to use inversions, so let's be a little more daring here. For instance, we could instead harmonize this passage as
Now I could have just picked those chords randomly from my available options, but I actually didn't; there's a method to the madness! First of all, you'll notice that
V is a sequence of descending fifths in the root of the chords. Whenever you can harmonize with chords where the root descends by a fifth, it will sound very satisfying (you'll learn more about this when you talk about secondary dominants.) Second, you'll see that I choose to take the
V to a
iv6 instead of a
I, borrowing from the structure of a deceptive cadence. My other option was to move directly to the
IV64, but, as you pointed out, it is unusual in this style of harmony to move from a dominant chord to a subdominant chord, so I eliminated that option. Lastly, I choose to switch from a
vi6 to a
IV64 on the third to last note in order to set up the final cadence. I didn't have to do this, but it makes things a little more interesting to not always line up the harmony one-for-one with the bass line.
One more thing to consider: second inversion (64) chords can be a little tricky and should be used with care. Typically, second inversion chords are only used in particular ways. Let's talk very briefly about three common ways you will probably encounter in basic harmonization: passing, neighbor, and cadential. A passing second inversion occurs when you have stepwise bass movement, for instance:
I6. A neighbor second inversion occurs when the bass line stays on the same note while the harmony shifts, for instance:
I. A cadential second inversion occurs when you use the following very common cadential figure:
I. This is by no means an exhaustive list, but it's good to keep an eye out for these opportunities to use second inversion chords when scanning your bass line.
In short, for all the notes between the cadences, you have some freedom as to how you'd like to structure the harmony, and your options only grow as you learn more and more theory. The good news is that with practice all of these patterns become internalized and you can quickly assess your options and make a decision. Until then, however, the best method is to write out all three options for each chord and then work backwards from the cadences, eliminating the options that won't work and choosing your harmony from the remaining options based on your own aesthetic judgement.
Some Additional notes as per your comments
Can a cadence be set up anywhere in a phrase? For example, how you set up the deceptive cadence between the two measures?
A cadence is specifically at the end of the phrase. When I wrote about using a deceptive cadence, I meant that I was simply alluding to that type of cadence in my chord choices. It's not actually considered a cadence unless it falls at the end of a phrase.
My book tells me to not use triads built on the III and VII grades of the scale in Major and Minor and the II in the Minor mode for now. Do you know why?
Triads build on the seventh scale degree in major and the second scale degree in minor turn out to be diminished triads (
ii°), which I suppose you haven't learned how to resolve yet. I'm not sure why you're being asked not to use
iii in major though. It's pretty common to procede a
vi with a
iii, but I suppose they want to teach you how to handle voicing that specific case before you start using that chord. Chords on the third scale degree can be tricky in harmonic minor, as you end up with an augmented chord (
III+), but you probably haven't reached that yet.
Should I incur in errors while harmonizing, like parallel/hidden 5ths and 8ves, would the best solution be changing the whole chord, or are they solvable without doing that?
In most cases, you can avoid parallel and hidden 5ths and 8ves by revoicing the chord, rather than changing the harmony, although there are some cases where you may just need to change your harmony to get out of a jam.