If it shuts down nearly immediately or just on battery power before it reaches 0% then its got a faulty battery, if its a removable one then youd have to buy a replacement.
If it shuts down during gaming or heavy programs then its a cooling issue, do you hear any fans, a low speed fan is good, high means its hot, no speed usually means that there is something wrong with the fan, open it up and clean it using a can of compressed air.
If none of these then go to the power settings and change the time out settings.
Or you can just check out these methods to see if one will help.
Be alert for scammers posting fake support phone numbers and/or email addresses on the community. If you think you have received a fake HP Support message, please report it to us by clicking on "Flag Post".