Normally a dog's penis becomes exposed when they become excited and mount or hump an object. If he isn't neutered, you may want to consider this as often this helps with humping behavior
A dog's penis is unique. It has what is called a Bulbus Glandis. This is located near the end of the of penis. When a male dog manages to get his penis inside the female and starts thrusting, the bulbus glandis enlarges to a round firm shape inside the female. This along with the female clamping down prevents the male and female from separating right after ejaculation thus ensuring the sperm remain in the female for at least this period of time. This tie can last from 5 minutes up to an hour. Here is a site with a great picture of male anatomy.
However, sometimes this gland will enlarge with just humping behavior and cause a dog's penis to become stuck outside of the prepuce (outer covering) until the gland shrinks back to normal size. Most dogs do not want to walk or move much when this is the case. In other cases, the hairs of the prepuce may have become caught preventing the penis' retraction. This can cause problems if it is not corrected. You can try putting a little lubrication on the penis such as KY jelly and gently roll the prepuce back and forth and see if the penis will return to it’s normal position. It may look a little weird in public, but will help the penis retract and get your dog walking
If it is hair getting caught, once it is in the normal position, trim the hair away from the opening to help prevent it in the future. If it is the bulbus glandis, there isn't much you can do away from home. If at home, you can make a saturated solution of sugar water using warm water. Mix the sugar into hot water until some sugar is left after mixing. Take a hand towel and soak it well and place it on the exposed penis. Be sure it is not too hot. This will sometime draw the fluid out of the bulbous gland and reduce the swelling allowing the penis to retract.
If the penis ever stays erect and will not retract using above methods, you should have him seen by the vet. The vet may have to put the dog under anesthetize to lubricate and return the penis to it’s normal position. You can read about this here:
I hope this information is helpful to you. If you need more information or clarification, please reply and I'll get back to you as soon as possible. If you are satisfied, please take the opportunity to rate.