The one question that decides most cases
Does this image have large areas of flat colour and hard edges?
Yes — screenshots, logos, diagrams, charts, text as an image, line art. These need a lossless format. Lossy encoders put visible haloes around every hard edge and smear coloured text, at every quality setting. Use PNG or lossless WebP.
No — photographs, continuous gradients, complex artwork. These suit lossy encoding, which exploits the noise and irregularity that make them photographic. Use WebP or JPEG.
Almost every format mistake on the web is one of these two put in the other's container: a photograph in PNG, which is five to ten times larger than it needs to be, or a screenshot in JPEG, which is both dirty-looking and often larger than the PNG would have been.