Protect your personal information when you get rid of electronic devices

We store virtually everything on our phones and laptops;  emails, texts, credit card, banking information. What if you want a new phone? How do you wipe the old one clean?