Why is $\oint_S \mathbf{E}\text d\mathbf{s}=q_{in}/\epsilon_0$ valid when finding electric field inside charged sphere(charges are inside sphere)? Let $r < R$, where $r$ is the distance to a point from the center of the sphere where we want to find electric field and $R$ is the radius of the sphere.
Why can we use $\oint_S \mathbf{E}\cdot\text d\mathbf{s}=q_{in}/\epsilon_0$ ($S$ is the surface of a Gaussian surface) when there are charges outside of the Gaussian surface? Aren't charges outside of the Gaussian surface affecting the electric field?
 A: actually the flux through a closed surface is both due to the charges inside and those outside
$\oint_S \vec{E_{in}}d\vec{s}+\vec{E_{out}}d\vec{s}=\frac{q_{in}}{\epsilon_0}$
but
$\vec{E_{out}}d\vec{s}$ vanishes as the field lines both enter and exit the surface.
You can also check out Newtons proof using calculus.Its pretty neat.
https://en.wikipedia.org/wiki/Shell_theorem
A: This was once a doubt for me too. But after asking my professor about it, he said this:
For the question, yes the charges outside will affect the field. But for the contradiction with the answer, I have an explanation below. 
From Gauss's law we say that the net flux through any closed surface is given by:
$$\phi = \frac{q_{in}}{\epsilon_o}$$
Now considering a spherical surface of radius $R$ and having a charge $q_1$ placed at the center and another charge $q_2$ at a distance $r (r > R)$ outside the surface. Like so:
 
Where $P$ is a point just outside the gaussian surface. 
Now, using gauss law, one would say that the field would become $\frac{q_1}{4\pi\epsilon_o\ R^2}$ from 
$$\oint_S\vec{E} d\vec{s} = \frac{q_{in}}{\epsilon_o}$$
$$\implies \vec{E}(4\pi R^2) = \frac{q_1}{\epsilon_o}$$
$$\implies \vec{E} = \frac{q_1}{4\pi\epsilon_o\ R^2}$$
But actually this field derived is what we can call as mean field on that surface. This is can be understood with the help of following diagrams:

These are the field lines and it can obviously be interpreted that the field would not be uniform.

And from here it can be seen that the fields are in opposite direction, and hence cancel out some part of each other and hence the mean field comes out to be of that value. 
A: This actually depends on how the charges outside the sphere of radius $r$ are distributed.  Gauss’ law works because one argue that the $\vec E$-field on the Gaussian surface has constant magnitude, as explained in this answer.  If 
the charges outside your sphere have a spherically-symmetric distribution, then their net overall contribution on any sphere is smaller radius will be $0$: in effect the field generated by a small amount of charge close to your point of interest will exactly be cancelled by the field generated by a greater amount of charge but farther away.
This is basically because the amount of charge in an area grows like $r^2$, but their contribution to the field decreases like $1/r^2$, and the two effects exactly cancel out.  You can see an illustration of this in the figure below.  

You can imagine the point is located on a sphere of radius $r$.  The amount of charge on the surface of the cone on the nearest part of the big sphere is proportional to the surface
of the cone that intersects the large sphere, and is given $a^2d\Omega$ if the distance from the charge to that near portion of the surface is $a$.  The contribution of all charges in the cones is thus proportional to the amount of charge divide by the distance $a^2$
$$
a^2d\Omega \times \frac{1}{a^2}= d\Omega \tag{1}
$$
and is thus independent of the distance $a$.  This is exactly balanced by the larger amount of charge in the opposite cone, a distance $b$ from the point.  The amount of charge in that far cone is proportional to $b^2d\Omega$ (it's the same opening angle) and thus the field from those guys is proportional to 
$$
b^2d\Omega \times \frac{1}{b^2}= d\Omega
$$
and exactly cancel out the contribution from (1) because of the opposite direction of the fields from the close and far charges.
If the charge distribution is NOT spherically symmetric outside your sphere of radius $r$, one cannot make this argument and there will be no cancellation.
A: The field IS affected by charges outside $S$. The flux is not.
Notice that
$$\oint_S\mathbf{E}_1\cdot d\mathbf{s}=\oint_S\mathbf{E}_2\cdot d\mathbf{s}$$
does not imply
$$\mathbf{E}_1=\mathbf{E}_2.$$
A: The simplest way to resolve this question might be the superposition principle. 
For simplicity, let $S$ not crossing any of the charges. Then the $E_{tot}=\sum_iE_i$ for charges $i$ at any points. Under the condition mentioned here:
https://math.stackexchange.com/questions/1149514/is-the-integral-of-the-sum-really-the-sum-of-the-integrals and the gauss law. It was very easily shown that $\int_S E dS\epsilon_0$ was of the charge inside. 
