My question is why don't they just always annihilate into photons, and what determines whether they decay into photons or gluons or something else?
I think it is important to add to the chosen answer this crucial difference between electromagnetic and strong vertices, it is not just trajectories, it is mainly the coupling constants.
The difference in the probability of seeing photons in a quark antiquark interaction or seeing gluons is due to the coupling constants difference, 1/137 for electromagnetic interactions, 1 for strong. Generally strong wins in probabilities.
As for the pi0 , have a look how it decays, it is the different coupling constants that control the probabilities, see the e+ on e- annihilation. ( Charge pions go through the weak interaction, neutral through the electromagnetic,that is why their lifetime is so short with respect to charged, see the weak coupling. In the case of charge pion the masses of W and Z also depress probabilities due to the propagators)