What are anal glands and Why do dogs have them ?
How to properly express a dogs anal glands:


You should express glands in the tub. First begin to wash the dog and use the shampoo to help your hands glide over the dog’s rear end.
Face dogs rear away from you.
Lift dogs tail, rolling it over to expose rear end.
Locate anal glands (near anus at approximately 4 o'clock and 8 o'clock)
If the glands are full you should feel a slight bulge when press inward just below anus.
Squeeze gently you will see and smell the foul fluid or pasty discharge.
Repeat process until glands are empty.
Once you are done wash dog's rear end well with a odor reducing shampoo, to reduce odor.
Note: Never use fingernails, do not press hard or you may injure the dog.

Tips
Smaller dogs will need this procedure done more often than larger dogs.
Dog will most likely become more energized after procedure is complete.

What are the anal sacs and what is their purpose?
The archaic anal sacs are an organ of the past and have no purpose in modern pets. The anal sacs are paired organs located beneath the skin and anal sphincter muscles at about the five and seven o'clock positions surrounding the rectum . Tiny ducts lead to the tissue just inside the rectum. The secretion that comes from these glands is brown in color and about the consistency of water or oil. The secretion has a very disagreeable odor to humans. As the pet defecates, the anal sphincter squeezes the sacs against the hard passing feces and causes the anal secretion to discharge onto the fecal mass. In olden times, this was used to mark a unique scent on the BM. It was like a sign-post used to communicate with the other dogs and cats.
Why so many problems?
You will notice in the above description that the anal sphincter must squeeze the sac against hard feces to express the contents. My idea of why modern pets have so many problems is that the feces are just not hard enough. In ancient times, dogs and cats ate large quantities of meat and bone making their feces the consistency of concrete. Now, we feed diets with vegetable protein as the main ingredient. This produces a much softer stool so there is nothing for the glands to be squeezed against. The secretions build up and the pet has problems.