The rationale for using the test statistics of the form $Q = \sum_i\frac{(X_i-E_i)^2}{E_i}$ is that counts $X_i$ in level $i$ of a categorical variable (univariate or mulivariate) are roughly Poisson-distributed with rate $\lambda_i$. Then $E(X_i) = Var(X_i)$ $\approx \lambda_i,$ so that
$$Z_i = (X_i - E_i)/\sqrt{E_i} \stackrel{aprx}{\sim} \mathsf{Norm(0,1)}$$
for sufficiently large $\lambda_i$ (estimated by $E_i$) and $Z_i^2$ is approximately $\mathsf{Chisq}(\nu = 1).$
If the contributions $Z_i^2$ to the "chi-squared" statistic $Q$ were independent, then $Q$ would be distributed approximately $\mathsf{Chisq}(\nu = K),$ where $K$ is the number of levels of the categorical variable. However, the $E_i$ are typically estimated from one or more totals, imposing linear constraints and reducing the number of degrees of freedom.
There are several approximations involved. Perhaps the most important ones are that $X_i$ are Poisson, that
$(X_i - \lambda_i)/\sqrt{\lambda_i}$ are nearly normal, that $E_i$ are good estimates of $\lambda_i,$ and that the linear restrictions are correctly accounted for in the degrees of freedom ascribed to $Q.$
Note: (1) The likelihood ratio test (involving products and logs) is more accurate and the theory leading to an approximate chi-squared distribution rests on firmer ground. But the test statistic is messier to compute, the procedure is more difficult to visualize, and there are no 'Pearson Residuals' to ponder for post hoc analysis if $Q$ is large enough to warrant
rejection of the null hypothesis on which the $\lambda_i$ are based.
(2) See also.