Most likely because your dog is trying to clean out their anal glands.
This is normally done naturally when your dog has a bowel movement. Provided the dog has a good “firm” stool.
The anal glands are located (on a clock) at 4 and 8 and when they have a bowel movement the stool pushes against the glands and releases them.
You may notice a slight amount of liquid as your dog has a bowel movement. This is the glands releasing. However, you will not always see this and it does not mean the glands are “not” cleaning out.
Some dogs do have issues and need their anal glands cleaned out periodically. This can be for other reasons than a constant loose stool.
Your vet can show you how to do this at home.
The gland at 4 and 8 (on a clock) will feel like the size of a B.B. You need to gently grasp them from either side with your fingers and gently squeeze to empty them. The odor can be quite sever so you may prefer to go to the vet for this.
The best way is to insert one finger in the rectum to properly release the gland.
You may also find yourself in a situation where your dog is so scared (by another dog, or other situation) where you suddenly small an awful odor. This is usually the dogs fear that automatically releases the anal glands.
If your dog is scooting in the yard or on your carpet you need to address this anal gland issue with your vet.
Impacted anal glands left uncleaned can result in surgery.