How (or when) do gluons change the color of a quark?
The idea that baryons contain three quarks is a significant oversimplification wrong. It works for some purposes, but in this case it causes way more confusion than it's worth. So you should stop thinking of baryons as groups of three quarks and start thinking of them as excitations in quantum fields - and in particular, excitations in all the quantum fields at once. Quark fields, gluon fields, photon fields, and everything. These excitations propagate through spacetime and convert among each other as they go, and in a baryon, the propagation and mutual conversion happen to sustain each other so that the baryon can exist as a coherent particle for a while.
One of the conditions required of all these excitations in fields is that they be a color singlet, which is the strong interaction's version of being uncharged. There's a simple intuitive justification for this: just as an electrically charged particle will tend to attract oppositely charged particles to form neutral composites (like protons and electrons attracting each other to form atoms), something which has the charge associated with the strong interaction (color charge) will attract other color-charged particles to form neutral composites (color singlets).
Now, if you literally only had three quarks, the only way to make them a color singlet is to have one be red, one be green, and one be blue.1 (Or the anticolor equivalents.) But with all the complicated excitations that make up a baryon, there are all sorts of ways to make a color singlet. You could have three red quarks, a green-antired gluon, and a blue-antired gluon. Or two red quarks, two green quarks, an antiblue antiquark, a blue-antired gluon, and a blue-antigreen gluon. Or so on; the possibilities are literally infinite.
The point is that you don't actually have to have a quark of each color in the baryon at all times. Only the total color charge in the baryon matters.
Given that, it should seem reasonable that gluons change the color of quarks whenever they are emitted or absorbed, in a way that keeps the total color charge the same. For example, a blue quark could absorb a green-antiblue gluon and become a green quark.
1I'm glossing over some quantum-mechanical details here; specifically, a color singlet wavefunction needs to be an antisymmetrized linear combination, like $\frac{1}{\sqrt{6}}(rgb - rbg + gbr - grb + brg - bgr)$, not just $rgb$. But as long as you don't worry about which quark is which color, for purposes of this answer it's safe to ignore this.
The model you are thinking about is really rudimentary and cannot explain the dynamics of Quantum ChromoDynamics, QCD .
In this link there is a better exposition of what a proton is, within QCD.
You may have heard that a proton is made from three quarks. Indeed here are several pages that say so. This is a lie — a white lie, but a big one. In fact there are zillions of gluons, antiquarks, and quarks in a proton. The standard shorthand, “the proton is made from two up quarks and one down quark”, is really a statement that the proton has two more up quarks than up antiquarks, and one more down quark than down antiquarks. To make the glib shorthand correct you need to add the phrase “plus zillions of gluons and zillions of quark-antiquark pairs.” Without this phrase, one’s view of the proton is so simplistic that it is not possible to understand the LHC at all.
In the over all counting balance the proton is color neutral and it is the huge number of gluon exchanges that generate the potential that keeps the three valence quarks tied up as a proton. Because it is a quantum dynamic framework the mathematics is not simple and has to be approximated by lattice QCD .
But in the process of the gluon moving from one quark to the next, wouldn't the baryon have two quarks of the same color, making it unstable
The stability is ensured by the innumerable exchanges between all those constituents. Color transfer from one quark/antiquark is compensated by the color remaining behind, the overall remaining color neutral.
how does this process actually work
It is essentially a quantum mechanical process, could be imagined by a summation of an infinite number of Feynman diagrams of this type exchanging gluons
The over all colors do not change adding up to color neutral for the whole bag.
To be fair one should explain that there do exist distributions within the proton that show the existence of valence quarks and sea quarks and gluons in the parton distributions, but that is another story.
Figure 2: Overview of the CTEQ6M parton distribution at Q = 100 GeV
The enhancement on large x is the "proof" of valence up and down quarks.
A short answer that's useful when thinking of individual quarks works:
The gluon also has color. There are 8 different forms, to go with the 3 colored quarks.
So if you start with a red and blue pair of quarks, the red changes to blue and you have two blue quarks and a gluon that’s red and anti-blue. Then the second quark absorbs it, cancels out the blue/anti-blue and is left with red itself.
Now the exchange between quarks is with virtual gluons, so don’t worry about it knowing where it’s going ahead of time. It’s the same issue you have in explaining how photons carry the momentum beween charged objects.