The moment generating function is defined by
Recall the power series expansion for the exponential function:
Then by extension, the MGF could be similarly written as
There's a certain theorem (due to Fubini, in case you're interested in learning more about it) that let's us exchange the order of integration (recall the definition of expectation for continuous random variables) and summation, so that
and by the linearity of expectation,
and here we see where the name MGF comes from: the coefficient of the n-th order term in the series expansion "generates" the n-th moment, which is defined as E[Xⁿ].
Now, recall the definition of variance:
and this is exactly the difference between the second moment and the square of the first moment.
So if you know the MGF, then you essentially get the variance for free with little effort. By differentiating the MGF, we get
and setting t = 0 lets us recover the first moment, E[X].
Differentiating again gives
and setting t = 0 once again recovers the second moment.
Then in terms of the MGF, we have