One possibility is that you have Face ID set up on the phone. When you use that, your face is used to unlock the phone (and it might seem like it wasn't locked as you didn't have to do anything other than look at the phone to unlock it).
If Face ID is on you would need to use the passcode to unlock the phone after you restart it. See: Restart your iPhone - Apple Support
To change the time before the screen automatically locks, go to Settings > Display & Brightness > Auto-Lock.
If that wasn't the cause, go to Settings > Face ID & Passcode > Turn Passcode On to setup a passcode. See: Set a passcode on iPhone - Apple Support
and see the following to also setup Face ID: Set up Face ID on iPhone - Apple Support