Why is electric flux through a cube the same as electric flux through a spherical shell?

Consider the flux through a tiny segment of a sphere. Since the electric field is parallel to the normal of the surface at all points, the flux is simply the electric field at that distance multiplied by the area of the element.

conical section

Now imagine tilting the top of the cone by an angle $\theta$ so that the corners still lie on the conical section, as seen below:

tilted conical section

The area increases by a factor $\frac{1}{\cos\theta}$, however the electric field vector in the normal direction $E_n$ is decreased by a factor of $\cos\theta$. Therefore the flux through this surface is unchanged since flux is the product of the normal electric field component and the area.

Now imagine splitting the cube up into lots of these conical sections. Clearly the tilting of the top surfaces of these sections due to the fact it being a cube rather than a sphere does not affect the flux flowing through each area element. Therefore the total flux flowing through the cube is the same as a sphere.

Note that this was a simplified adaptation from a chapter of The Feynman Lectures on Physics which explains why the images do not quite match my explanations since I was just talking about the top surface of the conical section being tilted. Feynman explains the effect of the flux through a closed surface in a more complete way.

Why does electric flux through a cube is same as that of electric flux through a spherical shell?

This is not only true for a cube or a sphere. The flux passing through any closed surface enclosing a net charge $q$ is $q/\varepsilon_0$. This is based on Gauss's law for electric charges.

When the field lines emerge from a point charge uniformly in all directions, the flux passing through any closed surface depends on the relative number of field lines which go into or out of the surface. For a charge inside the surface, the field lines either go out or come in depending upon the fact whether the charge is positive or negative respectively. For an external charge the net number of field lines which go in or come out of the surface is zero and hence it's flux contribution is zero.

So it doesn't matter whether it's a sphere or a cube (or even anything else), as long as a net charge of $q$ lies inside it, the total flux passing through the surface is $q/\varepsilon_0$. Also even if only one charge is present, it's not necessary for the charge to be at the geometric centre of the Gaussian surface.

The net flux is the same, but this doesn’t mean the flux is uniform.

Think of a similar situation where you place a lightbulb inside a closed lampshade. The net flux is the total amount of light passing through the lampshade. This depends only on the amount of light produced by the lightbulb, not by the position of the lightbulb.

In other words, if you can take a 60W lightbulb and move it anywhere inside your (closed) lampshade, and this will not change the total amount of light that goes through the lampshade. Of course unless you place the light bulb exactly at the center of a spherical lampshade, the amount of light will be NOT be uniform on every surface of your lampshade, but that not the net flux, which is the sum total of light of all the light on the entire lampshade.

Note I didn’t discuss the shape of the lampshade or its size. The net flux is determined by the strength of the source, not by the surface through which the light passes.