By using variational wave functions and quantum Monte Carlo techniques, we investigate the interplay between electron-electron and electron-phonon interactions in the two-dimensional Hubbard-Holstein model. Here, the ground-state phase diagram is triggered by several energy scales, i.e., the electron hopping $t$, the on-site electron-electron interaction $U$, the phonon energy $omega_0$, and the electron-phonon coupling $g$. At half filling, the ground state is an antiferromagnetic insulator for $U gtrsim 2g^2/omega_0$, while it is a charge-density-wave (or bi-polaronic) insulator for $U lesssim 2g^2/omega_0$. In addition to these phases, we find a superconducting phase that intrudes between them. For $omega_0/t=1$, superconductivity emerges when both $U/t$ and $2g^2/tomega_0$ are small; then, by increasing the value of the phonon energy $omega_0$, it extends along the transition line between antiferromagnetic and charge-density-wave insulators. Away from half filling, phase separation occurs when doping the charge-density-wave insulator, while a uniform (superconducting) ground state is found when doping the superconducting phase. In the analysis of finite-size effects, it is extremely important to average over twisted boundary conditions, especially in the weak-coupling limit and in the doped case.