On the hardware malfunction front, it's also possible that there's a problem with the USB cable you're using to connect the iPhone to your computer or power adapter. The only way to test this is to get access to another iPhone cable and try using that one instead. If you find that it is your USB cable that's broken, you can buy a new one from many stores.
If you're charging your iPhone using a wall charger power adapter (rather than by plugging it into your computer), it could be the adapter that's preventing your iPhone from charging. Just like with the USB cable, the only way to check this is by getting another power adapter and trying to charge your phone with that (alternatively, you could also try charging via a computer instead). Ideally you can borrow one, but if you need to buy a new one, there are lots of options.
4. Check USB Port
Once you know you're using the right kind of USB port, if you still can't get a charge, it may be the USB port itself that's broken. To test this, try plugging your iPhone into another USB port on your computer (or on another computer if you have one nearby). If that other computer recognizes and charges your iPhone, the USB ports on your computer may be broken.
You can also try plugging in another USB device that you know for sure works. That can hep you rule out that the problem is with your USB ports.
To make sure iPhone charges properly, you need to make sure you're charging it in the right place. Because the iPhone has high power demands, it needs to be charged using high-speed USB ports. The USB ports that are included on some keyboards don't provide enough power to recharge the iPhone. So, if your iPhone doesn't seem to be taking the charge, make sure it's plugged directly into one of your computer's USB ports, not the keyboard.
7. Check For Lint
This isn't a super common problem, but it's possible that lint from your pockets or purse could be jammed into either the iPhone's Lightning connector or your USB cable. If there's enough lint there, it could be preventing the hardware from connecting properly and thus stopping electricity from reaching the iPhone battery. Check your cable and dock connector for gunk. If you find it, a shot of compressed air is the ideal way to clear it, but blowing will also work.
8. You've Got a Dead Battery
If none of those things work, the truth almost certainly is that your iPhone's battery is dead and needs to be replaced. Apple charges US$79 plus shipping for the service. Spending some time at a search engine will turn up other companies that provide the same service for less. It's worth remembering, too, that if your iPhone is less than one year old, or if you have AppleCare, battery replacement is covered for free.