There are various methods used in compressing a file but they all operate on the same basic principle. A program looks at the file and uses a math formula (algorithm) to substitute a value in place of the data in the file.
As an example, lets do a "compression" on an image of the American Flag. The Flag has a field of blue at the upper left with stars in it and red and white stripes that are to the right of and below the blue field.
Compression will start with the upper left corner of the flag. The blue field is one hunded pixels across till it reached the first stripe. Currently, each pixel occupies one information slot in our file. A compression program may look at the flag and decide that it can represent the first line in a more compact way by noting that the color blue extends for 100 pixels and encoding it as "blue 100". Now, instead of taking up 100 slots to create the first blue line, only two slots are used, one for the color, another for the number of pixels. So you can see, we have compressed the fist line by 98%.
When it is time to display the Flag, the program will read the compressed file and will add 100 blue pixels to the top corner, thus uncompressing the file.
This is an overly simplified way of explaining it and the math is far more complicated but I think this will give you the general idea of how file compression works.