I don't understand Chi squared I think of the contingency table test. In all textbooks I've seen, the test statistic is calculated as the sum of $(O-E)^2/E$ over all cells. But the degree of freedom is not the number of all cells. E.g. in an $n \times m$ table, it's $(n-1)(m-1)$.
So far so good. But what's the rationale behind this approach? I think that the definition of the $\chi^2$ distribution is the sum of $M$ variables, each a square of a standard normal variable. I think that each $(O-E)^2/E$ is approximately standard normal, and that's why we use $\chi^2$ distribution. But the degree of freedom is not the number of summands! 
 A: The degrees of freedom is not the number of cells because there are relationships among the cells. 

I think that each $(O−E)^2/E$ is approximately standard normal,

No, for two reasons. First, it's the square of something that's normal, and second, the thing it's the square of is not actually standard normal*. The easiest case to consider is the multinomial (chi-square goodness of fit). To work out the expectations, we condition on the total observed ($\sum_i O_i=\sum_i E_i$). Given the total number of observations the variance of $(O_i-E_i)$ is not $\sqrt{E_i}$ but $\sqrt{E_i(1-p_i)}$. 
* whether we're dealing with a multinomial goodness of fit or a test of independence in a contingency table. 
However, the counts are negatively dependent in such a way that $\sum_{i=1}^k (O_i−E_i)^2/E_i$ is equivalent to a sum of $k-1$ independent standard normals.
In the $n\times m$ table, the relationships involve a linear restriction on every row and column, but one of those restrictions is implied by the others, so it's $nm-n-m+1$ df.
