If it hurts your foreskin when you attempt to retract it, you have a form of phimosis.
If it hurts during sex/handjobs/oral sex/masturbation etc., go get it checked. Additionally, it's important to clean underneath your foreskin. It can be a high risk area for infection if it's not cleaned properly.... especially in the odd chance you tear your foreskin for some reason.
In any case... pain isn't good. Generally, the foreskin is supposed to retract and if it hurts to do so, seeing the doctor woudl probably be a good idea.