The Lagrangian comes from a few places.
One is, you take a classical field that has a Lagrangian, and write the corresponding equivalent for quantum field theory. That only takes you so far, since there isn't really a good classical field theory for electrons. You can get the E&M part of QED this way, but you get stuck fairly quickly for the electron parts.
You also get stuck doing this with gravity, but a different way. The General Relativity Lagrangian converted to quantum formalism gives you a system that is not renormalizable.
Another place Lagrangians come from is starting with the quantum equation that seems to work. So the Dirac equation is a starting place for electrons. You write the Dirac equation and then you try to figure out what Lagrangian it corresponds to.
Another source is, you start with the interactions you know exist from experiments. So you go "backwards" from the interactions, and try to figure out what the Lagrangian should be that gives you the appropriate Feynman diagrams. So, for example, if you have an interaction where a photon comes off a charged particle line, then that gives you a psi-bar-A-psi term.
Another is the symmetries that should exist. If you have a gauge symmetry, then you write down a generic object that has that symmetry. For relativistic invariance, you write down things that are scalars under relativistic transformations. For particle numbers or other quantum numbers that are conserved, you write down generic mathematical objects that conserve those. This brings in such things as SU(3) matrix reps, or unitary matrix reps, and so on.
Another thing is, you want the correct spin rep. Photons are spin-1, for example, and sit in a vector rep. Electrons are spin-1/2 and sit in a spinor rep.
Then there are few things around the edges. For example, you want to make sure that the eigenstates don't have inconvenient instabilities. If your vacuum is unstable, for example, the theory can exhibit things like spontaneous production of infinite particles out of the vacuum.