Do not use vinegar as one answer suggests. Vinegar contains some of the same elements found in urine. Dogs go to the bathroom where they smell they have done so before.
That same answer also implies that a dog going to the bathroom somewhere you do not want it to as a sign of the animal being untrained or not cared for. This is an absolutely insulting answer and completely untrue.
As animals go to the bathroom based on scent, this is in no way a reflection of the animal not being cared for. A dog peeing in the same spot is instinct, and not always a training issue. If your dog has had an accident, or for some reason goes on the carpet, it will continue repeatedly until the carpet is properly cleaned.
To do this, your best cleaner will be a surfactant based one (I use Genesis 950). This type of cleaner actually breaks the stain apart and lifts it from the surface. The reason I use 950 is because it has an anti-bacterial agent which kills the germs and completely deodorizes the area - this is what eliminates the smell. I would highly suggest that you use it in a machine to get deep into the carpet.
Stay away from vinegar - it will only cause the cycle to be repeated and the "accidents" to keep happening. And just because your dog goes somewhere you don't want it to does not mean you do not care about your pet.