Whether you're selling your iPhone or taking it for repair you should reset it to delete your accounts and remove your content. Obviously, make sure it's backed up to iCloud or iTunes.
Factory reset an iPhone or iPad by going to Settings > General > Reset > Erase all content and settings. You will be prompted to enter your passcode and Apple ID before the process completes.
How to soft reset an iPhone or iPad
If your phone has locked up and you just want to restart it but none of the buttons work, then hold the power button and home button down at the same time until the device turns off and you see the Apple logo. Then let go and your phone or iPad will be back to normal.
For models which don't have a home button, such as the iPhone XS, press and release the volume up button, do the same for volume down, then press and hold the side button (sleep/wake) until the Apple logo appears.
Back up before you reset
There are occasions where you might want to factory reset your device not because you want to give it away but because it's playing up. Even if you use iCloud, before you do so it's sensible to make a full backup by connecting your iPad or iPhone to your computer, unlocking it, waiting for it to show up in iTunes, and then clicking on it. It should open the Summary screen, where you'll see a Backups section. Simply click the Back Up Now button. (Check the Encrypt iPhone backup box if you want all your passwords to be saved.)
Once that's done, you can begin the reset process. It's also worth remembering to remove your SIM card before you pass on the iPhone or iPad on, as this may have information stored on it, and you might want to continue using it in a new phone or tablet.
The version of iOS you are using may affect what you see here, but all are roughly the same - and things look the same on an iPad.
Step 1. Tap Settings, then General. Scroll down until you see Reset.
Step 2. Tap Erase All Content and Settings. Depending on whether you've set a Passcode and / or a Restrictions Passcode (they are different) you might have to enter that code or password in order to erase and reset it. There is no way to reset the device unless you enter the code(s).
Step 3. Now you will have to enter your Apple ID password. This is so the account can be removed from your device, and Find My iPhone can be switched off. If you merely restore an iPhone via iTunes, you'll be asked to enter the Apple ID and password when it reboots. Resetting it using the method we're describing here will prevent the iPhone asking for an Apple ID when it restarts.
Step 4. The reset process can take a minute or two, after which you'll see the welcome screen asking you to swipe to continue. It's then ready to be sold on, handed to a family member or taken to an Apple Store for repair.