There are many reasons why your cat may toilet in the 'wrong' place. We'll explore the reasons here, and offer suggestions for encouraging your cat to use their litter tray.
Always get your cat checked by your vet before trying to change their behaviour. A qualified behaviourist may be required if your vet, and the advice here, cannot resolve the issue.
Even if your cat has outside access, always provide litter trays inside. Some cats feel safer using a tray inside the house, particularly as neighbourhood cats may intimidate them. Your cat may be reluctant to toilet outside in bad weather or if the toileting site is frozen and they can't dig.