Why does <PERSON>' law (in integral form) hold for any closed surface? One way of looking at it is that it just does: it's one of <PERSON>'s equations, and there is no "proving" it. In other words, there is experimental evidence for it. Many people find it more natural to express it in the differential form ($\vec{\nabla}·\vec{E} = \rho/\epsilon_0$), which is equivalent to the integral form (through the divergence theorem) but doesn't refer to a specific surface. <PERSON>' law can be motivated by <PERSON>'s law, or even derived from it in electrostatics, but <PERSON>' law is more fundamental in the sense that it holds in electrodynamics while <PERSON>'s law does not.
Here is a somewhat lazy derivation of <PERSON>' law from <PERSON>'s law using the divergence theorem that also illustrates how $4\pi$ comes about. 346
In general it falls out of the math when dealing with situations of spherical symmetry.
From <PERSON>'s law, $$\vec{E}(\vec{r})=\frac{1}{4\pi\epsilon_0}\iiint\limits_\infty dV' \rho(\vec{r}') \frac{\vec{r} - \vec{r}'}{\lVert \vec{r} - \vec{r}' \rVert^3}.$$ Consider a region $V$ bounded by the closed surface $\partial V$. Then,
$$ \iint\limits_{\partial V}{\vec{E}(\vec{r})} · d\vec{S} =\frac{1}{4\pi\epsilon_0} \iint\limits_{\partial V}{ \iiint\limits_\infty dV' \rho(\vec{r}') \frac{(\vec{r} - \vec{r}')·d\vec{S}}{\lVert \vec{r} - \vec{r}' \rVert^3} }.$$
Changing the order of the integrals (which I won't attempt to justify), $$ \iint\limits_{\partial V}{\vec{E}(\vec{r})} · d\vec{S} =\frac{1}{4\pi\epsilon_0} \iiint\limits_{\infty}{ dV' \rho(\vec{r}') \iint\limits_{\partial V} { \frac{(\vec{r} - \vec{r}')·d\vec{S}}{\lVert \vec{r} - \vec{r}' \rVert^3} } }$$
Now consider the integral $$ I(\vec{r}') = \iint\limits_{\partial V} { \frac{(\vec{r} - \vec{r}')·d\vec{S}}{\lVert \vec{r} - \vec{r}' \rVert^3}} .$$ Suppose that $\vec{r}'$ is not enclosed by $\partial V$, so that the denominator is always non-zero and the integrand is always well defined. By the divergence theorem, $$ I(\vec{r}') = \iiint\limits_V {dV \vec{\nabla} · \frac{\vec{r} - \vec{r}'}{\lVert \vec{r} - \vec{r}' \rVert^3}} . $$ It's a straightforward calculation to show that the divergence in the integrand vanishes, therefore $I = 0$.
If on the other hand $\partial V$ encloses $\vec{r}'$, let $S_\delta$ be a sphere of radius $\delta$ centered about $\vec{r}'$ and contained in $\partial V$. Let $V - S_\delta$ be the subset of $V$ outside $S_\delta$, $\partial(V - S_\delta)$ the closed boundary of this region and $\partial S_\delta$ the spherical boundary of $S_\delta$. 804