This is typically caused by the headphone activation trigger being stuck. Your phone thinks there's a hands-free headset plugged in. You can check this by playing music on the iPhone; if it doesn't play through the speakers, the headphone trigger is stuck. Without opening the phone, the only "safe" way I know of to resolve this is to blow compressed air into the headphone jack.
If you feel like opening the phone, disassemble the phone until you can see the inside of the headphone jack. Dab some contact cleaner on the trigger and push it back and forth.