What really happens during a factory reset? Does it securely erase all data? When you "factory reset" an android phone, are you formatting the phone storage by securely erasing all data and writing over them with zeroes, or are you simply deleting the files by hiding them from the file system? If it's the latter, the data can still be recovered, correct?

If I'm trying to sell my phone, will a factory reset securely erase all my data, or will the next user still be able to recover it with some technical wizardry?
A factory reset reformats the phone's user-data partition, but it's not a "secure" wipe; it doesn't overwrite everything with zeroes.  If you want to be sure everything is erased, you can encrypt the phone first (which overwrites all the data with encrypted versions of itself), then do a factory reset (which sets up a new unencrypted filesystem).

(Update:  Most newer devices — those that shipped with Android 5 or later — are encrypted by default, so there's no need for a manual encryption step before wiping.  I expect that a factory reset should erase the master encryption key, rendering all the old data unreadable — though Google's documentation doesn't explicitly say so.)

Note that a factory reset leaves the phone's system partition (the "ROM") untouched.  If you've made any changes to the system partition (such as rooting), those will remain.