How each approach works
Lossy encoders exploit human vision: we notice brightness more than color detail and smooth gradients more than fine noise, so the encoder quantizes away what we'd miss least. A quality setting controls how aggressive that is. Once saved, the discarded detail is gone - re-saving at higher quality cannot bring it back.
Lossless encoders find repetition and predictable patterns and store them more efficiently, like a ZIP file that image viewers can read directly. Flat colors, text, and hard edges compress spectacularly; photographic noise barely compresses at all.
Choosing between them
Photographs: lossy, because the savings are huge and invisible at sensible quality settings. Screenshots, logos, diagrams, and anything with text: lossless, because lossy encoders smear exactly the hard edges those images are made of.