Derivation of mean speed from Maxwell–Boltzmann distribution
If one has a probability density function $P(x)$, then the expectation value of a quantity $f(x)$ is given by
$$\langle f \rangle = \int f(x)P(x)\,\mathrm{d}x$$
evaluated over the limits of the probability density function, i.e. if your PDF runs from $-\infty$ to $\infty$, then those are your limits of integration.
In our case, the PDF is the Maxwell–Boltzmann distribution (denoted $P(v)$ here) and the quantity we want to find the expectation value of is simply the velocity, $v$. Since velocity can only be positive,* the limits of integration are from $0$ to $\infty$.
Therefore, the mean velocity $\langle v \rangle$ is given by the integral
$$\langle v \rangle = \int_0^\infty v P(v)\,\mathrm{d}v$$
where $P(v)$ is the Maxwell-Boltzmann distribution
$$P(v) = \left(\frac{m}{2\pi kT}\right)^{3/2}4\pi v^2 \exp{\left(-\frac{mv^2}{2kT}\right)}$$
So:
$$\begin{align}
\langle v \rangle &amp;= \int_0^\infty v P(v)\,\mathrm{d}v \\
&amp;= 4\pi\left(\frac{m}{2\pi kT}\right)^{3/2} \int_0^\infty v^3 \exp{\left(-\frac{m}{2kT}v^2\right)}\,\mathrm{d}v
\end{align}$$
The integral can be evaluated using integration by parts repeatedly. The process is not interesting and you could consult a table of standard integrals to find the result:
$$\int_0^\infty v^3 \exp{(-\alpha v^2)}\,\mathrm{d}v = \frac{1}{2\alpha^2}$$
Setting $\alpha = m/2kT$,
$$\begin{align}
\langle v \rangle &amp;= 4\pi\left(\frac{m}{2\pi kT}\right)^{3/2} \frac{4k^2T^2}{2m^2} \\
&amp;= \sqrt{\frac{8kT}{\pi m}} \\
\end{align}$$
$m$ here refers to the mass of one molecule, whereas $M$ in your question refers to the molar mass of the compound. They are related by $M = N_\mathrm{A}m$, where $N_\mathrm{A}$ is the Avogadro constant. Since $R = N_\mathrm{A}k$, you can multiply top and bottom by $N_\mathrm{A}$ to obtain the desired result
$$\langle v \rangle = \sqrt{\frac{8RT}{\pi M}}$$

Footnote
* If the assertion that velocity is non-negative is confusing, it's worth noting that the $v$ in the Maxwell–Boltzmann distribution $P(v)$ is not the vector quantity $\vec{v} = (v_x, v_y, v_z)$, but rather the magnitude of the velocity $v = |\vec{v}| = \sqrt{v_x^2 + v_y^2 + v_z^2}$. Part of the derivation of the Maxwell–Boltzmann distribution involves converting from the vector quantity to its magnitude: that's where the $3/2$ exponent comes from (it's actually $1/2$ per dimension in three dimensions), and also where the factor of $4\pi v^2$ comes from ($4\pi v^2$ is the formula for the surface area of a sphere: loosely speaking, this factor represents the &quot;collection&quot; of all points $(v_x, v_y, v_z)$ which have the same magnitude $v$). A fuller explanation may be found in any typical physical chemistry textbook.