The three main file formats on the web are JPEGs, GIFs and PNGs. GIFs are most appropriate for images with flat colors and hard edges. JPEGs are in general best for photographs or other images with gradients. PNG is the most recent format and has some aspects in common with both the GIF and the JPEG. This format also has a lot in common with the TIFF format, which is a format used for storing images.
GIF (Graphic Interchange Format)
GIF compression is “lossless.” This means that no information is lost when the image is compressed. The compression scheme used is called LZW (for Lempel-Ziv-Welch), and takes advantage the repetition of data to create a kind of “shorthand,” concerning the information in a given image. They have 8-bit color information meaning there is a limit of 256 colors. GIF works best with flat colors and lines. They can be transparent and can be used to create simple animations.
JPEG (Joint Photographic Expert Group)
JPEG compression is “lossy” meaning that information is lost when the image is compressed. (Repeatedly saving and resizing a JPEG will cause the quality to degrade because of this. It is a good idea to keep the original image on hand and create new JPEGs from the original to keep this from happening.) JPEG handles images and photographs with shades and gradients very well. It does not do very well at all with flat colors or contrasts. JPEGs are 24-bit images.
PNG (Portable Network Graphic)
PNGs have a lossless compression scheme. They can be used for 8-bit indexed, 24-bit RGB, 16-bit grayscale and 48-bit images. Like GIFs, they can have transparent areas. PNGs cannot be animated. While it is possible to save photos with gradients in PNG-24, they usually have a much larger file size, so it is usually better to go with the JPEG. PNG-8 works very well for images that would normally be saved as GIFs.
This is just a general overview of these file formats. You can find more information about file formats in the following books:
Learning Web Design, by Jennifer Niederst Robbins
Web Style Guide 3rd Edition, by Patrick J. Lynch and Sarah Horton