You bought a used, carrier-locked iPhone without even knowing that it was carrier-locked, or getting the name of the carrier whom you should contact to unlock it?
As others have said, only the carrier can unlock the phone – if they choose to do so.
- If you are not already a customer of that carrier – chances are they will refuse to unlock it.
- If the previous owner bought the phone on a contract – with carrier financing – and the contract was not fully paid off – chances that the carrier will refuse to unlock the phone, unless, perhaps, YOU pay off the contract.
How to unlock your iPhone for use with a different carrier - Apple Support
There are some other kinds of "locks" that may raise their head that you will like even less than carrier lock. If the phone's previous owner did not part with it voluntarily, you may run into Activation Lock and/or carrier blacklisting, both of which are designed to turn lost and stolen phones into useless "bricks."
If the phone's previous owner did part with it voluntarily, but failed to prepare it properly for resale, the conditions might still be in place for Activation Lock to pop up.
I would suggest that you read
The All Too Common SAD Reality of Buying a Used iPad/iPhone – Apple Community