If you buy an ‘unlocked’ (not locked to any carrier) smartphone, then it is yours to do with as you see fit. Keep it or sell it. What to do before you sell or give away your iPhone - Apple Support
If you get a ‘locked’ smartphone as part of a plan with a carrier, then you have an obligation to keep that service for a period as stated in the contract. As the details differ per carrier, you will have to check with them.