In more detail
Lossless compression (PNG, GIF, lossless WebP) finds repeated patterns and rebuilds every pixel exactly, so it works best on screenshots, logos and flat colour. Lossy compression (JPEG, lossy WebP, AVIF) throws away detail the eye is least likely to notice, such as fine colour variation, and achieves much smaller files for photos. Lower quality settings shrink lossy files further but add visible artefacts: blocks, blur and halos around edges.
Why it matters
Large images slow down web pages and hit email and upload limits. Each time a lossy file is edited and saved again, it is recompressed and loses a little more quality (generation loss), so keep an original and export once. Resizing usually saves more than lowering quality: resize to the size you will display before compressing.
Example
A 4000 × 3000 photo has 12 million pixels; at 3 bytes per pixel it is 36,000,000 bytes uncompressed, while a good-quality JPEG of it is typically a few megabytes. Resizing it to 2000 × 1500 for a web page cuts the raw data to 9,000,000 bytes, a quarter, before any compression is applied.
Common confusion
Compressing makes the file smaller at the same pixel dimensions; resizing changes the Resolution and PPI itself.