Your equation should read:
$$\begin{align}
d\pi & = \theta\frac{\partial V}{\partial t}dt + \theta\frac{\partial V}{\partial S}dS + \frac{1}{2}\theta\frac{\partial^2 V}{\partial S^2}(dS)^2 +dS
\\
& = \left(\theta\frac{\partial V}{\partial t} + \theta\frac{\partial V}{\partial S}\mu S + \frac{1}{2}\theta\frac{\partial^2 V}{\partial S^2}\sigma^2S^2+\mu S\right)dt +\left(\theta\color{red}{\frac{\partial V}{\partial S}}\sigma S + \sigma S\right)dw
\end{align}$$
The only stochastic, i.e. risky term, in the equation above is:
$$ \left(\theta\frac{\partial V}{\partial S}\sigma S + \sigma S\right)dw $$
where $w$ is a Brownian Motion. Thus by setting:
$$\theta=-\frac{1}{\frac{\partial V}{\partial S}}$$
you cancel all stochastic terms and eliminate risk, therefore the portfolio must yield the risk free rate.
As an aside, note that the portfolio as defined here, $\pi = \theta V + S$ with $\theta=-(\partial V/\partial S)^{-1}$, is not strictly speaking self-financing $-$ check the comment by Gordon for more details.