Here are some steps to train your dogs NOT to do their business in places you don't want them to:
1. Find a fixed spot where you want your dog to do the business.
2. Keep an eye on your dog and observe the actions they do before they do the deed.
3. Do not shout/scold/spank your dog if they do their business indoors or places you don't want them to.
4. Familiarize your dog to the fixed spot for them to do their business.
Dogs are able to control their bladder better than puppies, making them easier to train. Most dogs mostly only release after meal times or in between intakes of liquid within the day. Make sure that after each intake, you lead your dog to the fixed area. Wait 15-20 minutes until they release and praise them like crazy.
Note: It is important that you DO NOT hold or carry your dog/puppy to the area. Pets might misunderstand the holding as affection. Affection must only be given AFTER they have done the deed on the right spot. If affection is given before the dog/puppy has achieved the goal, the idea of learning to go to the area decreases in your pets mind.