Why aren't all quarks clumped together in one giant hadron? As far as I am aware, the strong interaction is attractive only, and its carrier, the gluon, is massless meaning it has unlimited range. If this is the case, how come we only observe quarks in pairs and triplets? What's preventing every quark in the universe from attracting every other quark and forming one ever-growing mass of colored particles?
 A: In quantum chromodynamics, the theory that describes quarks, there exists a quantum number called color charge or just "color", and all stable hadrons need to have neutral or "white" color.
All hadrons need to have this zero total color charge because of color confinement or quark confinement, such that the color force, or strong force, works only at short ranges, but dramatically increases with distance.
Neutral color particles that exist naturally, are hadrons with a quark of one color and an antiquark of the corresponding "anticolor" i.e., mesons, or three quarks of different colors i.e., baryons, so that its net color is white.
For example, in a proton there exists a quark that is red, one that is green, and another that is blue. Overall the proton color is neutral, or white.
In 2013, scientists at CERN discovered using highly energetic collisions, the existence of a quickly decaying tetraquark, which is a state containing four quarks. There is speculation on how the four quarks are bound in this four-quark state. A lot of physicists tend to agree that these are two mesons $\mid q_1\bar q_1
+ q_2\bar q_2\rangle$ loosely bound to each other. The overall color is still neutral. But again, these are not stable structures. And also pentaquarks,  which were also made by high energy collisions, must have three quarks (eg., red+blue+green = white), and the two other quarks with color + anticolor (e.g., blue + antiblue = white).
So we see that quark forces are attractive only when in colorless states. We also see that quark states containing more than three quarks exist at high energies but nevertheless decay very quickly. There does not appear to be a reason why we could not synthesize particles containing multiple quarks (provided the overall state is colorless), though this would require high energy collisions. Given that the color force is very short range that increases in strength with distance, and since the average energy density of the universe is nowhere near the energy required for more than three-quark bound states, we do not see such states naturally.
A: The strong interaction is not "only attractive". A $qgq$-vertex has a color factor associated with it that depends on quark color and the gluon color/anti-color. The total color factor for a 2 vertex diagram is:
$$ C=\frac 1 2 c_1c_2 $$
A positive (negative) color factor is attractive (repulsive).
Interactions are as follows:

So here is $rr\rightarrow rr$, with two possible gluon states. The red quarks only interact with the red part of the gluon.
This interaction is repulsive:

Color swapping is attractive:

Now you have to combine red, green, and blue quarks, and also anti-red, anti-green, and anti-blue quarks. This process is governed by the representation theory of $SU(3)$, where $(r, g, b)$ are the fundamental representation, ${\bf 3}$, while $(\bar r, \bar g, \bar b)$ form ${\bf \bar 3}$.
They combine according to:
$${\bf 3} \otimes {\bf \bar 3} ={\bf 8} \oplus {\bf 1}$$
Graphically:

Meanwhile, a $qq$ baryon/meson hybrid doesn't work:

There is no singlet combination.
The Baryon sector is as follows:

where the red-dot on the far right represent the baryon color wave function:
$$\psi^c_{qqq} =
\frac 1{\sqrt 6}(rgb-rbg+gbr-grb+brg-bgr)
$$
At this point, you can compute the color factor for singlet and non-singlet states. It comes out attractive for singlets, and repulsive for other states. Nevertheless, it is a postulate that only color-singlet states are observed in nature..
Searches for more complicated singlets such as the pentaquark ($qqqq\bar q$) are on-going.
(Illustrations are from https://www.hep.phy.cam.ac.uk/~thomson/lectures/partIIIparticles/Handout8_2009.pdf).
A: I will start differently:
In the present cosmological model there exists a period called "the quark gluon plasma",. At that time  all the universe was a soup of quarks and gluons interacting continually with the strong force at very high particle energies because all the mass energy of the present universe was concentrated at a much smaller space time volume.  Experiments at LHC try to reconstruct energetically a quark gluon plasma, and there are indications of succeeding.
Lets start with the simple model of electromagnetic interactions that bind nuclei to atoms. Why do atoms exist and the electrons do not fall on the nuclei to neutralize them? Because of quantum mechanics, it was established and fitted with a quantum mechanical model that the hydrogen atom electron exists in specific energy states giving rise to the spectrum. One of the reasons for inventing quantization at the time was the discrete spectra of atoms. For hydrogen to form, the energies of the electron and proton have to be small so that the electron will fall to an energy state. High momentum electrons hitting protons have a very small probability to  make hydrogen.
The protons and neutrons are themselves bound state of quarks and gluons. The energies involved in order to get bound are of the order of MeV. Lattice QCD is working on these problems.
In the same way, nuclei are stable instead of collapsing due to the spill over  QCD force, called strong nuclear force, because bound states exist in the complex potential environment of protons and neutrons, the repulsive forces of the positive charge balanced by the attractive nuclear force, plus the Pauli exclusion because of spin states. The experimental fact is that there exist stable bound states. and one ends with the periodic table of elements, they are all quantum mechanical bound states.
When energies become large, TeV at LHC, quarks and gluons can behave as "free", as in the quark gluon plasma because it is no longer possible to stay bound into protons and neutrons, the probability is very small. In the quark gluon plasma of the universe, as the universe expands it cools and the average particle energies get of the order of MeV allowing the binding into protons and neutrons, at 1 microsecond after the Big Bang..
With the above as background, the answer to your:

What's preventing every quark in the universe from attracting every other quark and forming one ever-growing mass of colored particles?

is, that the present day quarks exist in stable bound states, which can only be broken with very high energy interactions.
A: There is a nice answer by @annav, and I would like to address something the other answers do not mention.
It all about the balance between the forces. You are asking for a giant object made of quarks? Would a neutron star or a quark star (theoretical) suffice?

Under the extreme temperatures and pressures inside neutron stars, the neutrons are normally kept apart by a degeneracy pressure, stabilizing the star and hindering further gravitational collapse. However, it is hypothesized that under even more extreme temperature and pressure, the degeneracy pressure of the neutrons is overcome, and the neutrons are forced to merge and dissolve into their constituent quarks, creating an ultra-dense phase of quark matter based on densely packed quarks.

https://en.wikipedia.org/wiki/Quark_star
You are basically asking why don't all the quarks in the universe clump together and create a giant hadron, so basically why do we see at our universe's current energy level the tetra and pentaquarks do be unstable?
To understand why, there are a few things to consider:

*

*The color force and distances

Let me ask you another question: "why don't all the nucleons (protons and neutrons) clump up together to form a giant nucleus?". The answer is very subtle, and has to do with the short range of the color force (and isospins, but I am not talking about this here). Basically, the protons (EM force) repel, but the residual strong force is trying to hold them together. As the nucleus grows in size, the residual strong force (because of its short range) cannot hold the protons together and the EM force overcomes it and the nucleus becomes unstable if it is too big.

It is true that whilst the strong force essentially only acts between nearest neighbours, whilst coulomb repulsion acts between all protons, it is actually the weak force that prevents the building of extremely large nuclei.

Why are heavier nuclei unstable?
It is very important to understand that the nuclear force (residual strong force) that holds the nucleons together and the strong force that holds the quarks together has a common underlying phenomenon, that is the color force. Though not completely analogously, but you can use this example of the short range of the color force for your case why quarks can't create stable objects above a certain size (or number of quarks).

What keeps quarks separate (strong force pulls, but what repels to equal out)
But there is another phenomenon of the color force, that is, that it becomes repulsive at very short distances. Why is this important? Because the quarks cannot be too close together, the object you build out of quarks simply has to grow is spatial size.


*Balance between the forces and the rule of lowest energy state

But why can't you just keep adding the quarks and grow this object? Because there are other forces, namely the EM force, and the weak force. The EM force in this case is complicated, because certain quarks attract, other repel, but the net of this is that as your object grows, they will first clump into stable neutrons and protons (where the EM and strong forces are balanced), and if you want to add other quarks to that, it will be energetically favorable for those quarks to simply clump separately into other protons and neutrons. And the weak force can transform quarks into other quarks to always make sure that the object takes the lowest possible energy state.
But why can't these neutrons and protons clump into a single quark-gluon plasma? They can, and theoretically, it is possible to turn a neutron star into a quark star, but how? You need energy, and pressure. You need to overcome the strong force's ability to become repulsive at short distances. And you need to reach an energy level, where it is not anymore energetically favorable for the quarks to clump into separate neutrons. Theoretically, there are two examples where this happens, one is the theoretical quark star. Or the other example of the early stages of our universe, where there was a quark-gluon plasma.
The answer to your question is that at the current energy levels of our universe, the quarks like to clump into separate protons and neutrons (and not giant objects) because this is the state of the lowest energy level (with the highest number of quarks), and this satisfies the balance between the forces, and this is compatible with the way the color force varies with distance, and all together these effects cause that it is energetically favorable for the quarks to clump into separate protons and neutrons.
A: As @josephh says in another answer, the stable combinations of quarks form colorless pairs or triplets, which means that the colors screen each other, rather like how the electron and proton in a hydrogen atom have opposite charges and so the atom as a whole is neutral and does not strongly attract other atoms at large distances.
Another factor is that gluons themselves carry color charge, and hence attract each other. This causes the field lines to "bunch up" into tubes between quarks, rather than radiating outwards as in electromagnetism. This causes the phenomenon of confinement which prevents quarks from being found on their own, and is another reason the color force is not observed over long distances.
