Lemma: If $A$ is a symmetric and idempotent $n\times n$ real matrix, then $A=UU^T$ where $U$ is an $n\times r$ matrix with orthonormal columns, $r$ being the rank of $A$.
Proof: Since matrix $A$ is idempotent, its eigenvalues are zero and one, and the multiplicity of unit eigenvalues equals the rank $r$ of $A$. Apply the spectral theorem for symmetric matrices to write $A=UDU^T$ where $D$ is a diagonal matrix of the eigenvalues of $A$ and $U$ is an $n\times n$ orthogonal matrix whose columns are the corresponding eigenvalues. Delete from $U$ the columns corresponding to zero eigenvalue, leaving an $n\times r$ matrix; note that $D$ then becomes the identity.
To prove the result, let's suppress subscripts. Assume $M$ is idempotent and symmetric. Use the lemma to find an $n\times r$ matrix $U$ with orthonormal columns such that $M=UU^T$ and $r$ is the rank of $M$. Write $N:=U^T\varepsilon$. Then $N$ has a multivariate normal distribution with mean vector $0$ and covariance matrix
$$
\operatorname{Var}(N)=E(U^T\varepsilon)(U^T\varepsilon)^T= U^TE(\varepsilon\varepsilon^T)U=I_{r\times r}.
$$
To finish, observe that
$$
\varepsilon^T M\varepsilon = \varepsilon^TUU^T\varepsilon=N^TN
$$
is the sum of squares of $r$ IID standard normal variables, and therefore has chi-squared($r$) distribution.
The lemma asserts that $r$ is the rank of $M$. Since $M$ is idempotent, the rank $r$ equals the trace. In the case $M=X(X^TX)^{-1}X^T$ this equals $\operatorname{tr}(M)=\operatorname{tr}[(X^TX)^{-1}(X^TX)]=\operatorname{tr}(I_{p})=p$.