Only charges through USB when screen is locked I have a Moto G running 4.4.4. 

When it's connected to a PC, it will only charge if the screen is locked: as soon as it's unlocked it will stop charging. In this situation, even if I lock the screen again it will not go back to charging.

When it's connected to the charger, it charges normally regardless of being unlocked or not.

What can be causing this problem and how to solve it?
The most likely cause is that your PC can't supply enough current over the USB connection to charge your phone while it's awake. Smartphones draw a lot of power these days, so their chargers use extensions to the USB standard to supply more power, so they can charge even while the phone is in use. Most PCs don't support these extensions, so they can't.

The solution is to use the charger to charge your phone. You can buy a Y-shaped USB cable which splits the power and data connections, so you can have it plugged into the PC and charger at once if need be. Some docking stations also have this ability.