Other people correctly answered how, although gluons are massless, their range appears to be very short. This happens because unlike photons, gluons have (color) charge, and therefore interact among themselves, and create color confinement.
A separate question is why are gluons massless. Well, all gauge bosons - photons, W/Z bosons, gluons, all start out massless. Higgs mechanism causes the symmetry in the Higgs field to generate excitations ("Goldstone bosons") which merge with some of the massless gauge bosons to create new particles - the W/Z vector bosons (massive), the photon (massless) and the Higgs scalar boson (massive). The gluons are unchanged by this mechanism (they do not have electric or weak charges, so they don't interact with the Higgs field), so they remain massless.