Essential dimension of moduli of curves and other algebraic stacks (with an appendix by Najmuddin Fakhruddin)