I'm not sure about the mathematical origins, but the original physical motivation was Yang and Mills's attempt to deal with the approximate SU(2)-symmetry of nucleons (protons and neutrons). The big step was (as I understand it) when Gell-Mann (and Ne'eman, independently at about the same time) realized that a diagram labeling experimentally observed particles was the weight diagram for SU(3). He made some predictions at a conference:
following the presentation on Strong interactions of strange particles by G. A. Snow, both Ne'eman and Gell-Mann raised their hands to ask for permission to speak. The chairman called Gell-Mann, who was the more eminent physicist of both, and Gell-Mann announced that "[...] we should look for the last particle called, say, Ω-, with S=-3, I=0. [Here, I is isospin.] At 1685 MeV it would be metastable and should decay by weak interaction [...]"
and the rest was the eightfold way.
Of course, principal $G$-bundles and the connections on them had been around for quite some time before (Simons famously pointed this fact out to Yang later on).