In this paper, for a consistent quaternion matrix equation AXB = C, the formulas are established for maximal and minimal ranks of real matrices X1, X2, X3, X4 in solution X = X1 + X2i + X3j + X4k. A necessary and sufficient condition is given for the existence of a real solution of the quaternion matrix equation. The expression is also presented for the general solution to this equation when the solvability conditions are satisfied. Moreover, necessary and sufficient conditions are given for this matrix equation to have a complex solution or a pure imaginary solution. As applications, the maximal and minimal ranks of real matrices E, F, G, H in a generalized inverse (A +Bi + Cj + Dk)- = E + Fi + Gj + Hk of a quaternion matrix A + Bi + Cj + Dk are also considered. In addition, a necessary and sufficient condition is derived for the quaternion matrix equations A1XB1 = C1 and A2XB2 = C2 to have a common real solution.