MacBook charger not working when laptop is on
Try the following steps: 


Try resetting the SMC which stands for System Management Controller, sometimes it can cause this type of problems. Here is a link to a good article from Apple on how to do it: Link
If possible, try an another charger and see if it charge properly with that one.
Take contact with Apple Support via phone or in your local Apple Store.


Hopefully any of these steps helped you in slowing your issue.