The litter box could be dirty, smelly, too small, in the wrong place, or too crowded. You may need multiple litter boxes spread around through the house. You may be using a type of litter your cat doesn't like. Another cat, dog, boyfriend, or baby in the house, or even some anonymous outside cat who has begun cruising the neighborhood may make your indoor cat feel that he or she has to start marking territory to make it clear who's place this is. It's sort of like people who write on restroom walls and other places.
Training is very difficult and punishment is probably a waste of time.
Neutering at an early age helps to prevent this behavior. Some cats also seem to pee on things to let you know something is wrong. A urinalysis should be done first. It is simple to collect some urine and the test is not very expensive. White blood cells and bacteria signify a bladder infection, and a bacterial culture should be done to select the best antibiotic.