How the exact size is reached
Each photo is saved over and over at different quality levels, narrowing in on the best quality that still lands under your limit. Only if the lowest sensible quality is still too big is the photo scaled down, and then just enough to fit with the quality kept high.
Why it lands a little under
Some sites count a kilobyte as 1,000 bytes and others as 1,024. Files are kept under the limit by both counts, so a 50 KB target gives a file of at most 50,000 bytes and it will pass either way.
JPG or WebP?
Choose JPG when you're uploading to a form, a government site or anything older, since it's accepted everywhere. WebP keeps more detail at the same size and suits websites and modern apps. Transparent areas turn white in JPG. Either way, the hidden camera and location details are left out of the new file.