>>12395466>>12395485 already stated a lot of correct things. One has to distinguish two types of symmetries under which a theory may be invariant: global and local. The former describe conservation of charges (e.g. electric charge), while the latter require the inclusion of gauge fields (e.g. the photon field)
There are two ways of looking at these symmetries: the first one is usually taught in Intro qft courses: firstly you obtain an Action of your theory (e.g. by fourier-transforming conservation of energy laws (read: schrödinger/klein gordon equation). Then you notice that your theory has a global symmetry implying there is some kind of charge associated. In the next step you make that symmetry local (usual argumentation: causality requires that you cannot make a transformation everywhere in space at the same time). In order to promote it to a local symmetry gauge fields (which mediate the changes in fields at certain points) have to be introduced.
The second way is a little more sophisticated and deeply rooted in differential geometry: firstly (no matter fields yet) you endow your spacetime with a local symmetry group (-> principal fiber bundle) and construct a connection. The meaning is roughly this: your symmetry group is attached to every point in spacetime, and local symmetry transformations change the basis which is employed for the group at every point in a different way. The connection relates the changes at different points. This connection (precisely the pullback to the base manifold) is the gauge field.
Matter fields play more of a secondary role, as they depend on the representation of (the Lie Algebra of) your local symmetry group and their behaviour is dictated by the underlying local symmetry.
(1/2)