Following Casella and Berger *Statistical Inference* derivation:

If the standard deviation of the population, $\sigma$, is unknown we can replace it by the estimation based on a sample, $S$, but then the expression of the one-sample t-test statistic will follow a $t$-distribution:

$$ T_{\text{test}}=\frac{\bar{X}\,-\,\mu}{S/\sqrt{n}}\sim t_{n-1}$$

with $$S=\sqrt{\frac{\sum(X_i-\bar X)^2}{n-1}}.$$

Minimal manipulations of this equation for $T_{\text{test}}$

$$\begin{align} \frac{\bar{X}\,-\,\mu}{S/\sqrt{n}} &= \frac{\bar{X}\,-\,\mu}{\frac{\sigma}{\sqrt{n}}} \frac{1}{\frac{S}{\sigma}}\\[2ex]
&= Z\,\frac{1}{\frac{S}{\sigma}}\\[2ex]
&= \frac{Z}{\sqrt{\frac{\sum(X_i-\bar X)^2}{(n-1)\,\sigma^2}}} \sim\frac{Z}{\sqrt{\frac{\chi_{n-1}^2}{n-1}}} \sim t_{n-1}
\end{align}$$

In the above expression,

$Z=\frac{(\bar{X}-\mu)}{\sigma/\sqrt{n}}\,\,\sim \,\,\small N(0,1)$

and as shown here:

$\sqrt{\frac{S^2}{\sigma^2}} =\large\sqrt{\frac{\frac{\sum_{x=1}^n(X - \bar{X})^2}{n-1}}{\sigma^2}} \,\,\sim \,\,\sqrt{\frac{\chi_{(n-1)}^2}{n-1}}$.

The derivation of the pdf of the Student's t distribution with $n$ degrees of freedom (**not** $n-1$ as above) proceeds by solving a **simplified problem**: finding the distribution of $U/{\sqrt{V/n}}$ with $U\sim N(0,1),$ and $V\sim\chi^2_n,$ mutually independent. The expression (1) is then replaced with the following expression ($k$ is used from this point on instead of $n$ for degrees of freedom) with the claim:

$$T=\frac{U}{\sqrt{\frac{V}{k}}}\sim t_k$$

With the premise of independence, the joint density of $U$ and $V$ is:

$f_{U,V}(u,v) = \underbrace{\frac{1}{(2\pi)^{1/2}} e^{-u^2/2}}_{\text{pdf } N(0,1)}\quad \underbrace{\frac{1}{\Gamma(\frac{k}{2})\,2^{k/2}}\,v^{(k/2)-1}\, e^{-v/2}}_{\text{pdf }\chi^2_k}$ with $-\infty<u<\infty$ and $0<v<\infty$.

Making the transformation $t=\frac{u}{\sqrt{v/k}}$ and $w=v$, hence, $u=t\,\left(\frac{w}{k}\right)^{1/2},$ and with $(w/k)^{1/2}$ as the Jacobian, the marginal pdf will be:

$$\begin{align}
f_T(t) &= \displaystyle\int_0^\infty \,f_{U,V}\bigg(t\,(\frac{w}{k})^{1/2},w\bigg)(w/k)^{1/2}\,\mathrm{d} w\\[2ex]
&= \frac{1}{(2\pi)^{1/2}}\frac{1}{\Gamma(\frac{k}{2})2^{k/2}}\,
\int_0^\infty\,
e^{-\frac{\left(t(\frac{w}{k})^{1/2}\right)^2}{2}}
w^{(k/2)-1}
e^{-(\frac{w}{2})}
\frac{w^{1/2}}{k^{1/2}}\,\mathrm{d}w\\[2ex]
&= \frac{1}{(2\pi)^{1/2}}\frac{1}{\Gamma(\frac{k}{2})2^{k/2}k^{1/2}}\,
\displaystyle\int_0^\infty\,
w^{((k+1)/2)-1}\,e^{-(1/2)(1 + t^2/k)w}\,\mathrm{d}w
\end{align}$$

The next step entails identifying in the previous equation the kernel of a gamma distribution pdf:

$x^{\alpha-1}\,e^{x\,\lambda}$

with parameters $\left(\alpha=(k+1)/2,\,\lambda=(1/2)(1+t^2/k)\right).$

The generic pdf for the gamma distribution is,

$\large \frac{\lambda^\alpha}{\Gamma(\alpha)}\,x^{\alpha-1}\,e^{x\,\lambda}$.

The strategy is then to synthesize the entire gamma pdf within the improper integral in our $f_T(t)$ pdf in progress, so that we can simplify it as just $1$, as we know to be true of all pdf's. To get away with it we need to multiply numerator and denominator by the same coefficient:

$\frac{\Gamma(\alpha)\,\lambda^\alpha}{\Gamma(\alpha)\,\lambda^\alpha}$. And since neither $\alpha$ nor $\lambda$ include the integrating factor $w$ we can include them inside the integral, or leave them out. Naturally, we want to leave within the integral $\frac{\lambda^\alpha}{\Gamma(\alpha)}$, and keep $\frac{\Gamma(\alpha)}{\lambda^\alpha}$ outside the integral. Now $f_T(t)$ will look hideous for just one second:

$f_T(t)=\frac{1}{(2\pi)^{1/2}}\frac{1}{\Gamma(\frac{k}{2})2^{k/2}k^{1/2}}\,
\int_0^\infty\frac{\left((1/2)(1+t^2/k)\right)^{(k+1)/2}}{\Gamma((k+1)/2)}
w^{((k+1)/2)-1}
e^{-(1/2)(1 + t^2/k)w}
\mathrm{d}w\;
\frac{\Gamma((k+1)/2)}{((1/2)(1+t^2/k))^{(k+1)/2}}$

... because everything between $\int$ and $\mathrm{d}w$ is just the gamma $\text{pdf}$ integrated over its entire support, so it becomes $1$, and we are left with:

$$\begin{align}
f_T(t)&= \frac{1}{(2\pi)^{1/2}}\frac{1}{\Gamma\left(\frac{k}{2}\right)2^{k/2}k^{1/2}}\,
\frac{\Gamma\left((k+1)/2\right)}{\left((1/2)(1+t^2/k)\right)^{(k+1)/2}}\\[2ex]
&=\frac{1}{(2\pi)^{1/2}}\frac{1}{\Gamma\left(\frac{k}{2}\right)\,2^{k/2}k^{1/2}}\,\Gamma\left((k+1)/2\right)\,
\Big[\frac{2}{(1+t^2/k)}\Big]^{(k+1)/2}\\[2ex]
&= \frac{\Gamma\left(\frac{k+1}{2}\right)}{\Gamma\left(\frac{k}{2}\right)}\,
\frac{1}{(2\pi)^{1/2}2^{k/2}k^{1/2}}\,
\Big[\frac{2}{(1+t^2/k)}\Big]^{(k+1)/2}\\[2ex]
&=\frac{\Gamma\left(\frac{k+1}{2}\right)}{\Gamma\left(\frac{k}{2}\right)}\,
\frac{1}{(2\pi)^{1/2}2^{k/2}k^{1/2}}\,
\frac{2^{(k+1)/2}}{(1+t^2/k)^{(k+1)/2}}\\[2ex]
&= \frac{\Gamma\left(\frac{k+1}{2}\right)}{\Gamma\left(\frac{k}{2}\right)}\,
\frac{1}{(\pi)^{1/2}k^{1/2}}\,
\frac{1}{(1+t^2/k)^{(k+1)/2}}\\[2ex]
&=\frac{\Gamma(\frac{k+1}{2})}{\Gamma(\frac{k}{2})}\,
\frac{1}{\sqrt{k\,\pi}}\,
\left(1+\frac{t^2}{k}\right)^{-\frac{k+1}{2}}
\end{align}$$

which is the $\text{pdf}$ of the $t$-Student or Gosset distribution with $k$ degrees of freedom (or $n$ degrees of freedom). Here is the Wikpedia expression:

where $\nu$ are the degrees of freedom.