Understanding the stability of surface nanobubbles