iPhone switches to speakerphone when away from my face

When you lay your phone on the table and dial a number, you should see this screen.

call screen

If you see speaker in blue, you might try tapping that part of the screen to see if you can return to the receiver for sound out. If so, hang up and repeat the call.

If all calls start with speaker, then you have a problem with the software or hardware. The Apple troubleshooting web site might help, but I'd do this first:

  1. Power down the phone and test after a clean restart.
  2. Back up the device (you will need to restore your data to the device after resetting things other than the settings in the immediate step below)
  3. Go to the Settings app > General > Reset > Reset All Settings
  4. Repeat the dial on the table test
  5. Now erase everything on the phone Settings app > General > Reset > Erase All Content and Settings
  6. Repeat the dial on the table test
  7. Restore the device using iTunes to erase it.
  8. Check the headset port and dock connector for foreign debris (or liquid damage)
  9. Get the hardware serviced by Apple (or other service provider) by going through the Apple Troubleshooting website mentioned before this list.