Dogs are pack animals, and they have a natural instinct to establish dominance. When two dogs meet, they will often bark at each other to see who is the alpha dog.
Dogs also bark to mark their territory. When a dog barks at another dog who is entering their territory, they are essentially saying "this is my space, stay away."
Dogs bark to protect their owners. If a dog sees another dog approaching their owner in a threatening manner, they may bark to warn the other dog away.
Sometimes, dogs bark at other dogs because they are afraid. This is more common in dogs who have had negative experiences with other dogs in the past.
Sometimes, dogs bark at other dogs simply because they are playing. This is especially common in puppies, who are still learning how to interact with other dogs.
In some cases, dogs bark at other dogs because they have a medical condition. For example, dogs with anxiety disorders may bark excessively at other dogs.
If your dog is barking at other dogs excessively, it may be a sign that they need more training.