There's multiple kinds of "locking" on devices. Click on the links below to find instructions on how to deal with your specific situation:
1) iCloud: Activation Lock - Activation Lock for iPhone and iPad - Apple Support - This is a security feature so only legitimate (e.g., not stolen) use of devices is possible. Read this support article about how to remove Activation Lock --> How to remove Activation Lock - Apple Support - If an activation lock was put on a device, only the owner of the account that applied the lock can remove it. Get them to remove it or return the item for a refund. If you are the owner and have the original Apple receipt to prove that, see the part at the end about asking Apple to reset the device.
2) Device lock: If you forgot the passcode for your iPhone, iPad, or iPod touch, or your device is disabled, read this support article if you are using iOS or iPadOS 15.2 or higher --> If you forgot your iPhone passcode or your iPhone is disabled - Apple Support or here if you do not meet the criteria for the previous method--> If you forgot your iPhone passcode or your iPhone is disabled - Apple Support
3) Carrier lock: Read this article about how to unlock your iPhone for use with a different carrier. --> How to unlock your iPhone for use with a different carrier - Apple Support Only some carriers offer the ability to unlock iPhone for use on any carrier. Follow the instructions in this article to unlock your iPhone. "To see the version of carrier settings on your device, tap Settings > General > About and look next to Carrier. To see additional carrier information, tap the version number."
If you buy a phone that has any kind of lock on it you may be in trouble. Even if you clear one kind of lock, others may still be present. A secondhand phone should arrive with all settings erased. If you get one that is not properly erased then either the seller was irresponsible or you may have bought a stolen phone and can never get it reset.