In this answer stability is covered first, thereafter the effect of the PID-controller on the stability.
Consider the following simple control system

which has a closed loop transfer function of $$\Gamma(s)= \frac{C(s)G(s)}{1+C(s)G(s)}=\frac{L(s)}{1+L(s)}.$$
Here $L(s) = C(s)G(s)$ is called the loop.
The system is unstable if at frequency $\omega$ where $\angle L(j\omega) = \arg\left(L(j\omega)\right)=-180^\circ$, the magnitude $|L(j\omega)|>1(=0$dB$)$.
Similarly, the system is stable if at frequency $\omega$ where $\angle L(j\omega) = \arg\left(L(j\omega)\right)=-180^\circ$, the magnitude $|L(j\omega)|<1(=0$dB$)$.
Furthermore, neutral (or marginal) stability is at frequency $\omega$ where $\angle L(j\omega) = \arg\left(L(j\omega)\right)=-180^\circ$, the magnitude $|L(j\omega)|=1(=0$dB$)$.
The stability margin is ''the space'' you have until the system becomes unstable (i.e. robustness).
Since stability is determined by using two measures: the gain and the phase, consequently there are also two stability margins (the gain margin and the phase margin).
The gain margin is ``the space'' you have at frequency $\omega$ where $\angle L(j\omega) = \arg\left(L(j\omega)\right)=-180^\circ$ until $|L(j\omega)| = 0$.
The phase margin is ``the space'' you have at frequency $\omega$ where $|L(j\omega)| = 0$ until $\angle L(j\omega) = \arg\left(L(j\omega)\right)=-180^\circ$ until.
The gain, phase and corresponding margins can be computed by hand, however I would recommend using software (e.g. MATLAB), since they can be very extensive.
To find effect your controller $C(s)$, you can draw Bodediagram and see the effect of the various parameters (MATLAB command bode()
):
The margins of the complete system can also be visualized using MATLAB (command margin()
), which should give more insight in the effect of the chosen controller and your stability margins.
