The idea is to make a global symmetry local. Let's start with the free Dirac equation, which follows from the action principle using the Lagrangian
$$\mathcal{L}=\bar{\psi} (\mathrm{i} \gamma^{\mu} \partial_{\mu}-m) \psi.$$
Obviously this Lagrangian does not change, when we introduce new fields by
$$\psi'(x)=\exp(-\mathrm{i} \alpha) \psi(x), \quad \bar{\psi}'(x)=\exp(+\mathrm{i} \alpha) \bar{\psi}(x).$$
According to Noether's theorem there's a conserved charge with the four-current density given by
$$j^{\mu}=\bar{\psi} \gamma^{\mu} \psi.$$
Now if you want to make the Lagrangian invariant under a "local" gauge transformation, where ##\alpha=\alpha(x)## is dependent on the spacetime argument, ##x##, you need to introduce a connection to define a covariant derivative, i.e., a vector field ##A_{\mu}## and the gauge-covariant derivative
$$\mathrm{D}_{\mu} \psi = (\partial_{\mu} + \mathrm{i} q A_{\mu}) \psi.$$
We want it to be covariant in the sense that for
$$\psi'(x)=\exp[-\mathrm{i} q \chi(x)] \psi(x)$$
you have
$$\mathrm{D}_{\mu}' \psi'(x)=\exp[-\mathrm{i} q \chi(x)] \mathrm{D}_{\mu} \psi(x).$$
From this you get
$$\mathrm{D}_{\mu}' \psi'(x) = (\partial_{\mu} + \mathrm{i} q A_{\mu}') \exp(-\mathrm{i} q \chi) \psi(x) = \exp(-\mathrm{i} q \chi) (\partial_{\mu} + \mathrm{i} q A_{\mu}' -\mathrm{i} q \partial_{\mu} \chi) \psi \stackrel{!}{=} \exp(-\mathrm{i} q \chi) (\partial_{\mu} + \mathrm{i} q A_{\mu}) \psi.$$
Thus you have to transform the "connection" (or more physically the gauge field) as
$$A_{\mu}' = A_{\mu} + \partial_{\mu} \chi.$$
Then your Lagrangian is invariant not only under global but also under local gauge transformations,
$$\psi'(x)=\exp[-\mathrm{i} q \chi(x)] \psi(x), \quad \bar{\psi}(x) = \exp[+\mathrm{i} q \chi(x)] \bar{\psi}(x), \quad A_{\mu}'=A_{\mu} +\partial_{\mu} \chi,$$
provided you use a gauge-covariant instead of a partial derivative,
$$\mathcal{L} = \bar{\psi} (\gamma^{\mu} \mathrm{i} \mathrm{D}_{\mu} -m) \psi.$$
Writing it out you get
$$\mathcal{L}=\bar{\psi} (\gamma_{\mu} \mathrm{i} \partial_{\mu} -m )\psi -q A_{\mu} \bar{\psi} \gamma^{\mu} \psi=\bar{\psi} (\gamma_{\mu} \mathrm{i} \partial_{\mu} -m )\psi -q A_{\mu} j^{\mu}.$$
To interpret ##A_{\mu}## as the four-potential of the electromagnetic field, all you have to do is to add a "kinetic term", and this should be a gauge-invariant addition to the Lagrangian.
The natural gauge-covariant object given the gauge-covariant derivative is the corresponding "curvature", which you get from the commutator of covariant derivatives,
$$(\mathrm{D}_{\mu} \mathrm{D}_{\nu} -\mathrm{D}_{\nu} \mathrm{D}_{\mu}) \psi = (\partial_{\mu} + \mathrm{i} q A_{\mu})(\partial_{\nu}+\mathrm{i} q A_{\nu}) \psi -(\mu \nu) = (\partial_{\mu} \partial_{\nu} \psi + \mathrm{i} q A_{\mu} \partial_{\nu} \psi + \mathrm{i} q A_{\nu} \partial_{\mu} \psi + \psi \mathrm{i} q \partial_{\mu} A_{\nu} - q^2 A_{\mu} A_{\nu} \psi) -(\mu \nu)=\mathrm{i} q (\partial_{\mu} A_{\nu} - \partial_{\nu} A_{\mu}) \psi=\mathrm{i} q F_{\mu \nu} \psi.$$
Indeed, you can easily show that
$$F_{\mu \nu}=\partial_{\mu} A_{\nu} - \partial_{\nu} A_{\mu}$$
is invariant under gauge transformations, i.e.,
$$F_{\mu \nu}'=F_{\mu \nu}.$$
That's why you add
$$\mathcal{L}_{\text{gauge}}=-\frac{1}{4} F_{\mu \nu} F^{\mu \nu}$$
to the Lagrangian.
The equations of motion, following from the Euler-Lagrange equations, indeed give the inhomogeneous Maxwell equations in covariant form,
$$\partial_{\mu} F^{\mu \nu} = q \bar{\psi} \gamma^{\mu} \psi = q j^{\mu},$$
i.e., the electromagnetic current is
$$j_{\text{em}}=q j^{\mu}.$$