If your dog is house trained or potty trained to pee outside then it could be that your dog is just simply not wanting to go outside or let you know they need to pee.
Then they just go and pee on the carpet instead.
When you see your dog starting to pee on the carpet you make a loud noise and tell them NO you go outside to pee and not on the carpet.
Then take the dog immediately outside to pee and continue doing so until the dog stops peeing on the carpet.
It could also be that the dog is marking it's territory on the carpet or they might have a bladder infection or something else causing the dog to have a loss of bladder control.
If your dog isn't sick or doesn't have any bladder problems then you just need to redirect the dog outside to pee whenever you catch them peeing on the carpet.
Make sure to clean up the pee right after they pee so the scent won't be left there.
You can also spray some lemon juice onto the carpet where the dog is peeing and they'll avoid peeing in that area.