I like the advice Skelaki has given except for the leash correction..he's only four months old and leash corrections (if you are going to use corrections, which I don't) are only used when a dog already KNOWS what to do..your puppy (BABY) does not, yet.
Keep a chart, when the pup eats, drinks, has playtime, when he has a bowel movement, when he pees etc and whether it was an OUTSIDE pee or an accident. Make sure you are feeding him on a set schedule (2 or three times a day) and do not free feed him..this makes it very hard to anticipate his need to defecate.
No punishment is necessary, nor recommended.
Your pup has to EARN his freedom in the house through management on your part. If you cannot be ACTIVELY watching him or interacting with him he should be attached to you by a leash or in his crate. Set yourself a timer (related to the feeding chart) for every hour or so and take him out (on leash) pick a spot and when he crouches to go (either pee or poo) use your "cue" word..as soon as he is done reward him with a goodie and a good dog. If he doesn't go in five minutes or so take him back in and kennel him for another ten or fifteen minutes and then do it all again. If he goes and you reward him, then you can (if time allows) let him have some freedom in the kitchen or another small area with playtime or even for a little walk or play in the yard. If you teach him that none of this happens without "business" being done you will eventually have a dog that goes immediately upon going out. This is a real benefit when you are in a hurry or the weather is bad and you want to make it a short outside trip.
Puppies are often not considered fully housetrained until a year of age, some earlier and some later...if you set him up to succeed by anticipating his need to go, learning his signals (sniffing, circling) and taking him out RIGHT THEN he will not have the opportunity to have accidents in the house. The fewer accidents he has the faster the program will work. They have little concept of "inside/outside", it takes a while to learn this.
Puppies usually need to go: after sleep, a nap, before and after a playtime or training session, 20-30 minutes after a meal (refer to chart for working this out) and any other time they have been active or excited.
If an accident happens, bop YOURSELF over the head with a newspaper, clean up the mess with a good enzymatic cleaner and get back on schedule. Mistakes at this point are the fault of the human, not the pup.