Why is it valid to detrend time series with regression? It may be a weird question at all but as a novice to the subject I am wondering why do we use regression to detrend a time series if one of the regression's assumption is the data should i.i.d. while the data on which regression is being applied is a non i.i.d?
 A: Basic least-squares type regression methods don't assume that the y-values are i.i.d. They assume that the residuals (i.e. y-value minus true trend) are i.i.d. 
Other methods of regression exist which make different assumptions, but that'd probably be over-complicating this answer.
A: It's a good question! The issue is not even mentioned on my  time series books (I probably need better books :) First of all, note that you're not forced to use linear regression to detrend a time series, if the series has a stochastic trend (unit root) - you could simply take the first difference. But you do have to use linear regression, if the series has a deterministic trend. In this case it's true that the residuals are not iid , as you say. Just think of a series which has a linear trend, seasonal components, cyclic components, etc.  all together - after linear regression the residuals are all but independent. The point is that you're not then using linear regression to make predictions or to form prediction intervals. It's just a part of your procedure for inference: you still need to apply other methods to arrive at uncorrelated residuals. So, while linear regression per se is not a valid inference procedure (it is not the correct statistical model) for most time series, a procedure which includes linear regression as one of its steps may be a valid model, if the model it assumes corresponds to the data generating process for the time series. 
A: You're astute in sensing that there may be conflict between classical assumptions of ordinary least squares linear regression and the serial dependence commonly found in the time series setting.
Consider Assumption 1.2 (Strict Exogeneity) of Fumio Hayashi's Econometrics.
$$ \mathrm{E}[\epsilon_i \mid X] = 0 $$
This in turn implies $\mathrm{E}[\epsilon_i \mathbf{x}_j] = \mathbf{0}$, that any residual $\epsilon_i$ is orthogonal to any regressor $\mathbf{x}_j$. 
As Hayashi points out, this assumption is violated in the simplest autoregressive model.[1] Consider the AR(1) process:
$$y_{t} = \beta y_{t-1} + \epsilon_t$$
We can see that $y_t$ will be a regressor for $y_{t+1}$, but $\epsilon_t$ isn't orthogonal to $y_t$ (i.e. $\mathrm{E}[\epsilon_ty_t]\neq0$).
Since the strict exogeneity assumption is violated, none of the arguments that rely on that assumption can be applied to this simple AR(1) model!
So we have an intractable problem?
No, we don't! Estimating AR(1) models with ordinary least squares is entirely valid, standard behavior. Why can it still be ok?
Large sample, asymptotic arguments don't need strict exogeniety. A sufficient assumption (that can be used instead of strict exogeneity) is that the regressors are predetermined, that regressors are orthogonal to the contemporaneous error term. See Hayashi Chapter 2 for a full argument.
References
[1] Fumio Hayashi, Econometrics (2000), p. 35
[2] ibid., p. 134
