Electric charges have electric field lines that start or end at the charges. The electric field lines for positive charge start at the positive charge and spread radially outward, and if there is a negative charge, the lines end at the negative charge.
Therefore the electric flux through closed surface enclosing a net charge is not zero but the magnetic flux through a closed surface is always zero. However no evidence until now has been found for the existence of magnetic monopolies despite the research is ongoing, magnetic field lines always form closed loops; it means they never start or end at the poles but form closed loops.
You may think magnetic field lines start at the north pole and end at the south pole but that's not true, they continue their path even in the inside of the magnet to form closed loops. So if you form a closed surface, the net magnetic flux through the surface is always zero, that is the lines that enter the surface must come out the surface. Now we have Ampere's law for magnetic fields.
This is similar to Gauss's law but it is based on the line integral around a closed path instead of magnetic flux through a closed surface. The line integral is \(\oint \vec B \cdot d\vec l\) where \(d\vec l\) is the small element of length on the path. The line integral gives the sum of each \(\vec B \cdot d\vec l\) for the entire path. Or the line integral can also be expressed in terms of parallel compoenent of \(\vec B\) with \(d\vec l\), that is \(\oint B_\parallel dl\). Note that the circle in the integral sign represents it is over a closed path.
The magnetic field lines of a straight current carrying conductor always form circles around the conductor, that is the magnetic field at a point in the circle is always a tangent at that point or parallel to \(d \vec l\). It is a well proved fact that the magnetic field lines always form circles for a straight current carrying conductor. It can be demonstrated with the help of compass needles. The compass needles are placed randomly in any direction without current. When there is current in the conductor the compass needles tangentially align on the path.
Use right hand rule to determine the direction of integration path; point thumb in the direction of current and the direction of curled fingers determine the direction of integration path. We have already determined the magnetic field of an infinitely long conductor carrying current \(I\) at a perpendicular distance \(r\) which is
\[B = \frac{\mu_0I}{2\pi \, r}\]
We now determine line integral \(\oint \vec B \cdot d\vec l\) for a infinitely long straight current carrying conductor. The line integral goes around a circular path of radius \(r\) whose center is at the conductor. We know that at every point on the path, \(\vec B\) and \(d\vec l\) are parallel, that is the angle between them is \(0^\circ\). So, the line integral is simply \(\oint B\,dl\) for this case. And the magnitude of magnetic field is constant for the entire path, you know
\[\oint B\,dl = B\oint dl = \frac{\mu_0I}{2\pi\,r} (2\pi \, r) = \mu_0 I \tag{1}\label{1}\]
The above equation is called Ampere's law. Ampere's law does not depend on the shape of the integration path. In Figure 2 the integration path is not a perfect circle but encloses current \(I\).
For the more general case shown in Figure 1, we have
\[\oint \vec B . d\vec l = \oint B\,dl\cos \phi\]
We know \(dl\cos \phi = r\,d\theta\) and
\[\oint B\,r\,d\theta = B\,r\oint d\theta = \frac{\mu_0Ir}{2\pi\,r} (2\pi) = \mu_0I\]
If the integration path encloses multiple currents, the line integral around the closed path is the product of sum of these currents \(I_\text{enc}\) and \(\mu_0\). The magnetic field \(\vec B\) is the vector sum of all magnetic fields due to all currents.
\[\oint \vec B \cdot d\vec l = \mu_0I_\text{enc} \tag{2} \label{2}\]
The above equation is called Ampere's law for all currents enclosed by the closed path and it can be stated as
AMPERE'S LAW: The line integral of magnetic field over a closed path is equal to the product of \(\mu_0\) and total current enclosed by the path.
Note that the magnetic field due to current outside the closed path can also contribute to the vector sum of magnetic fields but the line integral due to any such current outside the closed path is always zero. It means there is no contribution by the magnetic field of current outside the closed path in the line integral and the line integral around the closed path is still equal to the product of sum of the currents enclosed by the closed path and \(\mu_0\). The current outside the closed path is not included in the sum even if the total magnetic field \(\vec B\) is the vector sum of all magnetic fields due to currents inside or outside the closed path. You can easily understand this with Figure 2, imaging the current is outside the closed path, then the line integral of \(d\theta\) is zero instead of \(2\pi\).
The line integral \(\oint \vec B \cdot d\vec l = 0\) does not mean that, \(\vec B\) is zero.
The magnetic field can still be out there but if you take the line integral around a closed path that does not enclose any current but the current is outside the closed path, the line integral is zero, and that obviously does not mean \(\vec B\) is zero.
It was initially thought that \(\vec B \cdot d\vec l\) is the work done by the magnetic force on a "magnetic charge" similar to similar to \(\vec E \cdot d\vec l\) (work done by the electric force on electric charge). But no magnetic monopole has ever been detected, the so called "magnetic charge" does not exist. And the magnetic force is always perpendicular to the velocity of the moving charge and it is also proportional to the velocity, that is \(\vec F = q\vec v \times \vec B\), therefore the work done by the magnetic force is always zero. It means the \(\vec B \cdot d\vec l\) is not related to the work done but it is related to the current if we determine its integral over a closed path enclosing that current. It also means the magnetic force is nonconservative force.
Furthermore the work done by the electric force over a closed path is always zero, it means a charge moves from one point and returns to the same point. The electric force is not proportional to the velocity of the moving charge. The electric field lines are the lines of forces, that is they exert force in the direction parallel or antiparallel to the field and therefore the work done over a closed path is zero. But the work done by the electric force if there is a net displacement, is not zero. Note that the electric force is is proportional to the position of the charge not the velocity. Therefore, the electric force is a conservative force (for now we consider that the electric field is conservative, later you'll see that the electric field can also be non-conservative in Faraday's law).
Ampere's law is useful for calculating the magnetic field of current configurations of high degree of symmetry, so that we can easily evaluate the integral of magnetic field similar to we used symmetry considerations in Gauss's law to determine the electric field even though the Ampere's law is valid for any current configurations.