Apple today posted a support document explaining what to do before you sell or giving away your Mac and how to back up and remove your data from your computer to keep the data safe:
Important: Do not manually delete contacts, calendars, reminders, documents, photo streams, or any other iCloud data while signed in to your iCloud account. Doing so will not only delete that information from your Mac, but will also delete it from the iCloud servers and any other devices you also sync with iCloud. Instead, follow the steps below to remove your iCloud account from your Mac and leave your iCloud data intact on your other devices.
1. Deauthorize your computer from iTunes (if applicable).
2. Back up your data.
3. If enabled, turn off Find My Mac and sign out of iCloud. To turn off the iCloud service on your Mac:
• Choose System Preferences > iCloud.
• Deselect Find My Mac to disconnect your devices from iCloud.
• Click the Sign Out button on the left side.
• The system automatically removes iCloud data from your Mac.
4. Reformat your hard drive with Disk Utility to erase all stored data and then reinstall OS X on your computer.
Optional: If you want to return your Mac to its original “out-of-the-box” state, so the new owner can set up the computer with the Mac OS X Setup Assistant, follow these steps:
1. After you reformat your hard drive and reinstall OS X, the Setup Assistant automatically starts and displays the Welcome screen that prompts you to choose your country or region. Do not continue with the setup of your system.
2. Press Command-Q to shut down your Mac.
Your Mac is now ready for its new owner. When the Mac is turned on for the first time, the Setup Assistant will guide the new owner through the setup process.
More info in the source article here.