The Root of the Behavior
These behaviors can be commonly observed in the animal kingdom. It is not just canines and the like but many different mammals. Any animal that uses pheromones to attract romantic partners, urine to mark its territory, or even skunks or protect themselves with their spray. Animals often communicate this way and many have heightened olfactory capabilities. Humans also communicate in this manner, though it is typically artificial now. We use cologne, perfume, and deodorant to smell more attractive to each other. In ancient times these scents would be the odors we naturally excrete from our pores. These odors would have been used to scare off potential predators back in the day. Now we have a more effective form of communication developed over the years called language.
We as humans may have evolved out of the need to excrete these chemicals to communicate but canines have not. It is actually one of their most used forms of communication. Using their empowered ability to smell, they will often check these glands on each other to make sure they do not need to be fearful or aggressive toward another canine. That is why they engage in this behavior whenever they meet a new canine or most unfamiliar animals. This is not their only form of communication. A dog's bark, posture, and other forms of body language all factor in when they are evaluating each other. The sense of smell just happens to be their most refined sense and therefore is their most accurate way of evaluation. Although to us it seems distasteful and gross, this is an important part of the greeting ritual and is not going to have any negative medical ramifications. Your dog will not get pink eye. Allow your dog to approach another dog and sniff, it will not take long and it will help to put both dogs at ease in a new situation.
Encouraging the Behavior
Understanding how chemical communication works gives us a little bit of insight as to how we should treat this behavior. When your dog greets another, allow them to approach without a leash as long as you do not suspect any violence or aggression from either. Pulling their leash or keeping it taut will escalate their anxiety and often impair their ability to evaluate the other canine. Don't punish or otherwise discourage this behavior. It is a natural and safe form of communication and any negative reinforcement will go unheeded. They simply will not understand why you are upset. Worse they may take your behavior as a warning and consider the other dog a threat.
Dogs in heat use this form of communication to attract potential mates. This was arguably the first humanity noticed and recorded this observation. Back in the time of ancient Greeks they observed the reaction a male dog had to the secretions to a female in heat. This observation led them to uncover a wide range of ways animals use different scents to communicate. Humans, through selective breeding, have likely helped to heighten the olfactory senses of canines. A dog with a sharp sense of smell would have been highly sought after during the early days of humanity. A dog that could hunt well would often be selected for breeding as to encourage the same traits in their offspring. This process over the course of thousands of years likely played a role in this heightened ability.
Other Solutions and Considerations
Dogs can be unusually perceptive to chemical abnormalities, to the point that they could tell if another dog was sick using scent alone. Any kind of medical deficiency that would obstruct the production of a certain chemical would come off substantially different then a healthy dog. Dogs will also use this form of communication with other animals. This explains why they will often come up and sniff a human butt or crotch. The odors that are excreted from these areas are naturally formed chemicals and in theory would give the dog some information on the human companion. What a dog could tell about a human from this sniff would be hard to evaluate but the same principles apply as they would to other animals.