How do we know the electrical charge of individual quarks?

There are processes where the quark charges show up fairly directly, such as the total hadronic cross section in electron-positron colliders (like LEP at CERN, which the LHC replaced).

Often the e[sup]-[/sup] and e[sup]+[/sup] in the “collision” will not interact and so carry on to go round the ring again. The total hadronic cross section essentially tells you how often that doesn’t happen; where you get anything at all coming out of the collision other than e[sup]-[/sup] and e[sup]+[/sup]. That might be simple or it might be messy, but you’re just counting the instances where something happens.
Glossing over (lots of) technicalities, it turns out that this is essentially given by the sum of the squares of the charges of the quarks that are lower in mass than the energy of the collision.

What’s then pretty neat is that as you go up in energy, a heavier quark can now become relevant, with the result that the cross section jumps up by an amount corresponding to the square of its charge. That’s a pretty direct measure of whether that particular type of quark has a charge of +/- 1/3 or +/- 2/3.
(Yes, I am glossing over all the messiness that happens close to the threshold.)