The other answers already offer you enough thoughts about how these compression schemes work. They try to find occurrences of patterns (longer or shorter ones) in any data, whether it's an image, a text, or any kind of file. And then the storing of the patterns plus the instructions to use them (together with any unique data) to reconstruct the file, needs probably less information then the raw data.
Many kinds of file formats (like ZIP,TIF, PNG, and GIF) use some kind of run length encoding. JPG even uses it in combination with lossy compression. Sometimes its called LZW (Lempel–Ziv–Welch), sometimes it's under a different name.
Oorspronkelijk geplaatst op Quora: How does lossless compression reduce file size?"