Using USB ports for charging when MacBook Pro is not connected to power source and with lid closed
You'll need to keep your MacBook Pro awake while the lid is closed. There is no setting within macOS that lets you override the default behaviour (enter sleep mode when the lid is closed).

You can use a free 3rd party app called InsomniaX. It lets you disable the sleep mode while the lid is closed.


  It always has been a missing feature: disabling the sleep mode on a Apple Laptop. Who does not want to use it as a big juke-box or go warwalking. The best looking server ever, especially at about one inch height. This small utility is what you will want, this small utility acts as a wrapper to the Insomnia kernel extension.
  
  


Image source: http://semaja2.net/ye-ol-projects/insomniaxinfo/