We first derive a nice formulation of $\sum_{i=1}^N(y_i-\hat{y_i})^2$:
\begin{align*}
\sum_{i=1}^N(y_i-\hat{y_i})^2
&=\|y-X\hat{\beta}\|^2\\
&=\|X\beta+\epsilon-X(X^TX)^{-1}X^T(X\beta+\epsilon)\|^2\\
&=\|(I_N-H)\epsilon\|^2\\
&=\epsilon^T(I_N-H)^T(I_N-H)\epsilon\\
&=\epsilon^T(I_N-H)^2\epsilon\\
&=\epsilon^T(I_N-H)\epsilon
\end{align*}
with the first equality by definition of $y_i$, the second equality by $y=X\beta+\epsilon$, the third equality by linear algebra manipulations, the fourth equality by definition of $\|\bullet \|^2$, the fifth equality by $H^T=H$, the sixth equality by the fact that $I_N-H$ is the orthogonal projection onto the complement of the column space of $X$ and the fact that projecting two times is the same as projecting one time.
With this nice formulation in hand, we have $(N-p-1)\hat{\sigma}^2=\epsilon^T(I_N-H)\epsilon$ with $H$ a projection matrix onto the column space of $X$. So, $I_N-H$ is the projection onto the orthogonal complement of the column space of $X$, which has dimension $N-p-1$. Suppose $v_1,...,v_{p+1}$ span the column space of $X$ and $w_1,...,w_{N-p-1}$ span its orthogonal complement, then
$$\epsilon^T(I_N-H)\epsilon=\sum_{i=1}^{N-p-1}(w_i^T\epsilon)^2,$$ which is the sum of $N-p-1$ standard normal variables, a $\chi^2$-distribution with $N-p-1$ degree of freedom.