A puppy isn't born knowing that your carpet is not an acceptable place to relieve himself. You are going to have to show it the ropes and train the puppy to hold it until it's time for a walk outside or playtime in the backyard. Here's a relatively easy way to train him that doesn't require punishment.
Watch your puppy's behavior while relieving himself outdoors so you can detect the warning signs and intercept him when indoors.
Stay outside as often as possible during nice weather so your puppy can develop a preference for eliminating outdoors. Help him develop a liking for surfaces like dirt and gravel by taking him outdoors to eliminate after eating, playing and sleeping, or, ideally, every 15 minutes.
When it's time, go straight to a predesignated area and don't leave until the puppy urinates.
Tuck your puppy into a cozy crate in your bedroom at night. Dogs are den animals and don't like to soil the area where they sleep.
Carry the puppy outdoors when he becomes restless in the middle of the night, and wait until he's finished relieving himself.
Supply a litter box (filled with sand or kitty litter) during the night, unless you plan on getting up every couple of hours to take him outside. If you do want to take him outside, set your alarm if you sleep too deeply to notice that your puppy has started fidgeting, and carry him outside at those times.
Carry the puppy outside first thing in the morning so he won't soil the floors as he walks outside.
Be consistent with training. Consult a pet behaviorist if you have problems.
Reward your dog with puppy treats and praise every time he successfully eliminates outdoors.
Corrections and punishments for indoor accidents will only teach your puppy not to eliminate around you (even when outdoors), but won't stop him from eliminating indoors when you're not around. If you catch the puppy in the act, say 'No!' sharply and carry the puppy outside.
Avoid giving your dog the message that relieving himself is wrong. Don't rub his nose in the mess, and don't hit him with a newspaper.