As the How has already been explained, you might want to have the What as well:
There are several lists on the internet (just google for "secret codes") with many of those codes. Not all of them work on all phones (some are device or manufacturer specific), but many are common to all Android devices (and some times even to all smartphones). I won't list all of them here, but a few more important to be known include...
┌──────────────────────────────────────────────────┬───────────────────────┐
| Code | Description |
├──────────────────────────────────────────────────┼───────────────────────┤
| *#0011# | GSM Infos |
| *#0228# | Battery Infos |
| **05*<PUK>*<new PIN>*<new PIN for confirmation># | Unlock PIN |
| *#*#0588#*#* | Test Proximity sensor |
| *#06# | Show IMEI |
| *#*#1234#*#* | Firmware Info |
| *135# | Show your phone number|
| *#*#197328640#*#* | Service Menu |
| *#*#7378423#*#* (*#*#SERVICE#*#*) | Service Menu |
| *#*#2432546#*#* (*#*#CHECKIN#*#*) | Check for OTA Updates |
| *2767*3855# | Factory Format |
| #31#<phoneNumber> | anonymous call |
| *#*#3264#*#* | RAM Test |
| *#*#4636#*#* (*#*#INFO#*#*) | System Infos |
| *#*#7594#*#* | Customize PowerButton |
| *#*#7780#*#* | Factory Reset |
└──────────────────────────────────────────────────┴───────────────────────┘
Use at your own risk -- some options might prove dangerous (e.g. the factory format) -- especially if you don't know what they are supposed to mean, think twice or better avoid the option.