Like any other mechanical system, an airplane is stable when it tends to go back to its initial attitude when something perturb this attitude. In particular, "an aircraft is longitudinally statically stable if a small increase in angle of attack will create a nose-down pitching moment on the aircraft, so that the angle of attack decreases"¹

Considering a conventional airplane (i.e. an airplane with a well defined wing, fuselage and tailplane) the main sources of the pitching moment $M$ are:
- lift $L$ and aerodynamic pitching moment $M_{wing}$ of the wing; lift acts in the aerodynamic center of the wing which lies at a distance $x_{ac}$ from the nose; aerodynamic center is at 25% of the chord at subsonic speeds and 50% at supersonic speeds; aerodynamic pitching moment $M_{wing}$ is normally negative and more or less constant with the AoA;
- aerodynamic pitching moment $M_{fus}$ of the fuselage;
- lift $L_{tail}$ of the horizontal tailplane, which acts at a distance $x_{ac_{tail}}$ from the nose;
- engine's thrust (which actually contributes with three different terms).
(source Wikipedia, modified by me)
Aerodynamic drag is normally omitted as well as the aerodynamic pitching moment of the tailplane; to simplify a bit our discussion, we omit also the contributions from the engine(s) and from the fuselage.
Now we need a point in respect to which calculate the total pitching moment $M$: we use the CG of the airplane since it gives some useful results. Then summing up all these terms we get:
$M=L(x_{cg}-x_{ac})+M_{wing}+L_{tail}(x_{cg}-x_{ac_{tail}})$
If we equal this equation to zero, we get the conditions needed to equilibrate i.e. trim our airplane. Anyway to get its stability we need to go a step further and apply the previous definition: the airplane is stable if any perturbation that changes the AoA creates a pitching moment $M$ that brings the AoA back to its initial trimmed value. This means that the aircraft is stable if a positive change of AoA creates a negative change of moment $M$, i.e. if their ratio is negative. Mathematically:
$\frac{∆M}{∆\alpha}<0$
where $∆$ means "variation". Applying this definition to the previous expression we get:
$\frac{∆C_M}{∆\alpha}=\frac{∆C_L}{∆\alpha}(x_{cg}-x_{ac})+\frac{∆C_{L_{tail}}}{∆\alpha}(x_{cg}-x_{ac_{tail}})\frac{V_h²S_h∆\alpha_h}{V²S∆\alpha}$
which is the "stability equation" of the airplane. A couple of comments before going further:
- as usual in the aerospace world, moments and forces have been adimensionalised via $½\rho V²S$ and written in coefficient form;
- $M_{wing}$ disappears; as said, wing's pitching moment is basically constant in respect to AoA and therefore its variation is zero and doesn't enter in the equation;
- the very last term just translate the fact that the aerodynamic characteristics of the horizontal tailplane must obviously be given in respect to its $(½V_h²S_h∆\alpha_h)$ while the adimensionalisation has been done in respect to the wing's $(½V²S∆\alpha)$; worthy of remark is the fact that both the speed $V_h²$ and the AoA $\alpha_h$ seen by the horizontal tailplane are different than the ones of the wing; this is due to the downwash of the wing impinging on the tailplane which reduces both.
Now we only have to set $\frac{∆C_M}{∆\alpha}<0$ to get the aeromechanical settings needed to have a stable airplane. Since we have the sum of two terms, in order for this equation to be negative at least one of these terms must be negative and more negative than the other. We start from the tailplane:
- $\frac{∆C_{L_{tail}}}{∆\alpha}$ this is the slope of the lift curve which is positive; $\frac{V_h²S_h∆\alpha_h}{V²S∆\alpha}$ this is the ratio of geometric entities and is also positive; $(x_{cg}-x_{ac_{tail}})$ if the horizontal stabiliser is located on the tail, then this term is negative; bingo! The horizontal tailplane gives a negative contribution to stability and therefore it is stabilising.
- $\frac{∆C_L}{∆\alpha}(x_{cg}-x_{ac})$ the slope of the lift curve is positive so this term is positive or negative according to the relative position of CG and AC; if the CG is in front of the AC then this term is negative (i.e. stabilising) and viceversa.
What do we choose now? CG first or AC first?
But I'm still confused about why transport aircraft generally have the center of gravity before the aerodynamic center.
If we want to be conservative then we put the CG in front of the AC: should the tailplane lose some of its effectiveness, the airplane remain stable. Plus, if the CG helps in stabilising the airplane then a smaller tailplane can be used giving less drag and weight.
Couldn't you put the c.o.g. behind the aerodynamic center, and trim the aircraft with an upward lift force on the tail?
Yes definitely, we can we put the CG between the wing and the tailplane but then 2. becomes positive (i.e.destabilising) and the tailplane needs to be bigger to compensate also for this instability due to the CG. Anyway this configuration with the CG between wing and tailplane entails that the horizontal tailplane produces a positive lift relieving a bit the job of the wing.
¹https://en.m.wikipedia.org/wiki/Longitudinal_stability