This can be either a Hardware or Firmware issue. You are not alone as there are hundreds of other iPhone users experiencing the same issue. Try reading this on an apple forum. Some possible fixes include -
- Plugging earphones in and out of headphone jack about 20 times. Some iPhones are shipped in "earphone mode" and need to be reminded that THEY ARE NOT
- Clean out dock port (possible dust has gotten inside and corroded port making iPhone think it is connecting to something continuously ) and completely restore iPhone. Use a dry toothbrush (something small with bristles to do the job) or dry compressed air. DO NOT BLOW on it with your mouth as saliva may get into it and make it worse.
- Disconnect the battery for several seconds and reconnect, this will physically reset ALL electronic parts of the iPhone. Repair Manual HERE.
- Upgrading to the new/latest iOS
If none of those options have worked then you will need to either take your iPhone back to apple for a replacement (if still under warranty) or replace the dock connector, Repair Manual HERE
Hope this helps :)