You need to have the dog on a feeding schedule. Feed him his kibble twice a day, once in the morning, and once at dinner time. Provide water all through out the day, but pick it up when it's bed time, just don't forget to put it back in the morning.
Also, you cannot put your dog out for X amount of time, and expect him to do his business, then come in and not potty. Dogs are like kids, they get distracted. They forget to do what they're supposed to.
You need to take your dog out to potty. Take him to the spot you want him to relieve himself, and while he's peeing or going poop say a command word, in a happy voice, such as "Gotty potty" and repeat it 'til they're done. You will also want to give treats for goin' potty outside.
Then, when you're inside training, train him in the spot he goes potty. This will help him to realize it's not a potty area.
Next, make sure he gets plenty of exercise. Walk him twice a day, at least for 30+ minutes each, and then still run around in the back yard and play with toys and train.
You will need to get all the areas he went potty in with an enzymatic cleaner. You can use Natures Miracle, or you can use a home made recipe. EIther way, until the potty areas are clean so your dog can't smell them (not just you) they will keep going back.
Homemade recipe:
Ingredients: water, vinegar, baking soda, peroxide, dish soap.
First, steam clean the area with a carpet cleaner, if you can, if not that's okay.
Next, mix 50-50 vinegar and water together. Soak the potty areas with this liberally. It needs to go through all areas of your carpet, and down to the padding underneath. Scrub with a scrubbing brush, then let this sit for at least an hour.
After that, blot up the vinegar with some paper towels or a regular towel, then sprinkle heavily with baking soda. Let that sit 30 minutes.
While the baking soda is sitting mix a half cup peroxide, with 1 TSP (teaspoon) vinegar and slowly pour that over the baking soda after 30 minutes.
Once all this is dry, vacuum up, you may need to use the scrubbing brush to loosen baking soda.
If you have very heavily soiled areas you may need to repeat this process. You can use a blacklight to find urine spots, or just stick your nose to the carpet and sniff.
Good luck! You've come to an excellent place.