I read here that $n-p-1$ was the number of degrees of freedom I should use when doing a t-test for the significance of a regression coefficient, but I don’t understand why. My understanding was that t-tests generally had $n-1$ degrees of freedom.
You lose one degree of freedom for each estimated mean parameter. For an ordinary t-test that’s 1 (the mean). For regression, each predictor costs you a degree of freedom. The extra one is for the intercept.
More specifically, the degrees of freedom come from the denominator in the t-test, which is based on the residual sum of squares — there are $n-p-1$ degrees of freedom in the residual sums of squares.