Polarized and unpolarized neutron triple-axis spectrometry was used to study the dynamical magnetic susceptibility $chi^{primeprime}({bf q},omega)$ as a function of energy ($hbaromega$) and wave vector (${bf q}$) in a wide temperature range for the bilayer superconductor YBa$_2$Cu$_3$O$_{6+x}$ with oxygen concentrations, $x$, of 0.45, 0.5, 0.6, 0.7, 0.8, 0.93, and 0.95. The most prominent features in the magnetic spectra include a spin gap in the superconducting state, a pseudogap in the normal state, the much-discussed resonance, and incommensurate spin fluctuations below the resonance. We establish the doping dependence of the spin gap in the superconducting state, the resonance energy, and the incommensurability of the spin fluctuations. We discuss in detail the procedure used for separating the magnetic scattering from phonon and other spurious effects. In the comparison of our experimental results with various microscopic theoretical models, particular emphasis was made to address the similarities and differences in the spin fluctuations of YBa$_2$Cu$_3$O$_{6+x}$ and La$_{2-x}$Sr$_x$CuO$_4$.