How do I reset my Android Samsung phone back to factory settings?

How do I reset my Android Samsung phone back to factory settings? I do not know how or why or what one of my multiple messaging apps has the bug but I am not able to receive a text message from Facebook to reacquire my Facebook account.