A closed surface like a sphere encloses some volume. Anything coming out through the surface (the net outward flow which we call the flux) will be in the expense of what remains inside. If the sphere encloses some charge, then electric field diverging out from the volume containing the charge will be equal to the normal component of the electric field lines through the surface, which we call the electric flux.
The vector flux will be zero if the boundary and the surface are parallel. The electric filed is a special type of a vector which has a non-zero divergence if there is some non-zero charge. The electric flux will be zero only if there is no charge enclosing that surface.
However if you place an uncharged sphere in a uniform electric filed, the sphere develops induced charges. But there the charge is not residing inside the sphere but on the sphere. i.e, the charge induced is not enclosed by the sphere. So in that case the charge inside the sphere remains zero and you will get zero divergence and zero flux.
The Gauss's law states that the total electric flux coming out from a volume of charges is equal to the charge enclosed divided by the permittivity of the medium inside the surface.
Yes, for any closed Gaussian surface (including irregular surfaces), this law holds.
But we consider only symmetrical surfaces (sphere, cylinder, etc.) in academics as it is easy for performing the integration.
Let us consider the example which you have given:
Black dot represents a point charge.
Here, in order to find the net flux through the surface you must integrate each
E.dA, which is not so easy. The advantage with symmetry is that E is always constant, and dA is always perpendicular to the surface so that you may integrate easily. Even in this case, if you integrate E.dA, you will end up with the same result.
Gauss law even applies for irregular surfaces :
The only problem is to integrate. You would have the same result.
How to think without mathematics :
As we know, flux is the measure of number of lines of electric field passing through the Gaussian surface. Even if you move the charge anywhere within the surface, the number of field lines won't change, right?
All the Gauss laws(of electrostatics, magnetism, gravitation), field vectors, inverse square laws can be derived from the GAUSS LAW OF DIVERGENCE.
It's so exciting!!!
Best Answer
Your question contains a contradiction. Basically the last sentence of your question is completely right. If $\mathbf{E}=f(x)\hat{x}$, then $$ \frac{\rho}{\epsilon_0}=\nabla\cdot\mathbf{E}=f'(x)\neq 0 $$ So in order to have an electric field that is only in the $\hat{x}$ direction and changes in magnitude with $x$, there must be charge present. With this electric field, Gaussian surfaces have nonzero flux because there is charge inside of them.
If there is no charge in a Gaussian surface, the net flux through it is zero, and this electric field is not possible. This is an absolute law. There are no exceptions.
This question reminds me of this other one, where OP thought they could specify the charge on a conductor and the voltage. Just like in that situation, the electric field (or rather the divergence of the electric field) is determined by the charge. You cannot freely choose any electric field and separately say there is no charge present.