Mounting behavior, also known as "humping" is a normal behavior and is often seen in puppies at play. It can also occur in older dogs, and though normal to dogs, it is generally objectionable to people. Both male and female dogs can display this mounting behavior. They may mount other dogs, people, or inanimate objects. In most cases it is not a sexual behavior, but is an attention seeking behavior, a way to act out anxiety or relief, or it could be self-rewarding. There are some medical conditions that could contribute to this behavior, so a dog showing mounting behavior should be checked by a veterinarian.
Let's always go back to the basics and make sure the dog responds to the basic verbal commands like "come", "sit", and "down." The dog should be rewarded every time he obeys the command. He should also learn to watch you when you give a direction. He should be prevented from doing undesirable behaviors. Another good command for him to learn is "Settle." To teach this command you start rewarding him for showing relaxed behavior while saying the word "Settle." Another good command is "Go, get your toy." If he learns to go get a toy when he hears this command, it can give him a positive alternate behavior.
Second, let's make sure the dog has an outlet for his energy. Does he live in an enriched environment with lots of toys and opportunities for exercise and physical activity with you? Dogs rarely get enough exercise when left to themselves, but must be encouraged by having the owners actively involved. Long walks, games of fetch, swimming, and other activities can do a lot to settle a dog's behavior. A common mantra among animal behaviorists is, "A tired dog is a good dog." It's not always true, but it comes very close.
If the mounting is occurring with other dogs, it is helpful to get your dog trained to a Gentle Leader, which will provide much more control. Only allow the dogs together when they can be supervised. Reward non-mounting behavior with tasty treats and provide lots of activities to keep the dogs' attention away from mounting. Use commands and the head halter to interrupt any undesirable behavior. The best thing is to prevent the behavior from happening. Therefore, if he even just appears to be thinking about it, find an alternative behavior for the dog like "Go, get your toy." or "Settle." Be sure you don't give him a toy or reward if he has already started mounting, since that will lead him to think he's being rewarded for mounting. You will need to be in control during the entire situation.
A similar scenario would apply if the dog is mounting children or adults. Only allow interaction between people and the dog when the dog can be supervised, preferably while wearing a head halter. Reward non-mounting behavior with tasty treats and provide lots of activities to keep the dogs' attention away from mounting. Use commands and the head halter to interrupt any undesirable behavior. Here, again, find an alternative behavior. Teach your dog that the appropriate way to greet a human is by sitting in front of them.
If your dog is the one being mounted, talk to the owner(s) of the other dog(s). If they are unwilling to train their dogs, you could teach your dog to sit when another dog intends to mount him or her.
In some instances, neutering or spaying a dog will decrease the mounting behavior, but this is not assured. If a medical condition such as a urinary tract infection or hormonal imbalance is the cause of the mounting, the condition should be treated.