Why are the degrees of freedom for a chi-square test on a 2x2 contingency table always 1? I know the "formula" is (# rows - 1) * (# columns -1).   However, shouldn't the design of the experiment or observational study being conducted make a difference?
The only experimental design I can think of where 1 d.f. makes sense to me is for a 2x2 table when both margins are fixed - such as in Fisher's famous "lady tasting tea" experiment.  Since both margins are fixed, we only need to know 1 of the 4 values within the 2x2 table to fill in the rest of the table, so only 1 value within the table is "free to vary" and therefore 1 d.f.
But what if instead we have an observational study, where only the total number in the sample is known?  Now we need to know 3 of the 4 values in the 2x2 table before we can fill in the last value.  So in this case, 3 values are "free to vary" and therefore 3 d.f.?
And finally, if we have 2 separate samples where the number in each sample is fixed by design, we need to know 2 values in the table before we can fill in the remaining 2 values; 2 are "free to vary" so 2 d.f.?
Apologies if above betrays a naive/incorrect understanding of how degrees of freedom actually work.  As I have attempted to research the topic, this idea of values that are "free to vary" made some intuitive sense to me, so I became curious as to why it wouldn't work as described above for chi-square tests.
 A: As far as I know, degrees of freedom in Chi Square distribution are related to the number of classes a population can be classified minus the linear restrictions used to estimate the parameters.
Originally, Karl Pearson provided Chi Square statistic to compare observed versus expected values in a contingency table, where you have a sample of size $n$ (this is always fixed) in $k$ different classes. He was based in the multinomial distribution, where sample size is fixed, and arrived at the expression used today of the sum of the squared difference between observed values and the expected, divided by the expected. At this point, we have $k$ classes and only $k-1$ of them "vary freely". 
In a 2 by 2 table, we have 2 variables (or two samples) with 2 levels and in each one we have $(k-1)=1$ that vary freely. The total number of cells that vary freely then is $(k-1)(k-1)=1$ again.
If you think of one single variable with 4 levels, that wouldn't fit in a contingency table: it's just one factor, and in that case it is correct that applying a Chi Square test to assess goodness of fit will have 3 df. 
A: This is a much more complicated question that it might first seem, and there was a bitter disagreement between Fisher and Pearson on this question.


*

*With modern computing it is easy to demonstrate by simulation that the distribution is $\chi^2_1$, not $\chi^2_3$, eg


> tests<-replicate(1000,{
+   y<-rbinom(400, 1, .2)
+   x<-rep(0:1,each=200)
+   chisq.test(table(x,y),correct=FALSE)$statistic
+ })
> mean(tests)
[1] 0.9988341
> var(tests)
[1] 1.870081



*A theoretical point: the marginal probabilities are ancillary, so we will be better off conditioning on them, which gives one df left over. 

*The distribution conditional on the margins is (in large samples) $\chi^2_1$ regardless of what the margins are, so the unconditional distribution should also be $\chi^2_1$, agreeing with simulations. 
A: If we have a 2x2 table with two variables and sample size n is set, then in essence, the marginal sums of those  two variables taken sepparately is also set as n. 
That comes from the fact that with marginals we only look at one variable and disregard the other completely.  We can imagine deleting the line that devide the columns (or the rows) like we never knew the other variable existed. If we do that, the two columns for each row  (or the two rows for each column)  get combined (and the numbers added) and n becomes the marginal sum of the rows (or columns) that were left devided as levels of one of the variables. 
That means for each variable now, we only need to fill 1 level of 2 levels, since the sample size n has become the marginal sum and once we know one frequency the other frequency is determined by n minus frequency number one. Hence the degrees of freedom formula.
The reason why we have to sepparate the two variables like that, and combine rows and columns is that we are interested in looking at how likely our observed deviance from the expected values is under the independence model when the variables are independet and the frequencies come from such a distribution(the null-hypothesis). To do that, we need to use the specific chi-squared distribution the shape of which is dictated by the independence model and the appropriate degrees of freedom. In a sense the independence model that we want to test requires the sepparation of the variables to calculate the degrees of freedom in the correct way.
A: We have a $M \times N$ contingency table, under H1: no association, there are $MN-1$ number of free parameters. Under H0: $p_{ij} = p_ip_j$, we have $(N-1) + (M-1)$ free parameters,
$$DegreeOfFreedom = MN-1 - (N-1) - (M-1) = (M-1)(N-1)$$
