If you could be more specific about what kind of computer it was, that would be a big help.
I have this problem with my Toshiba Qosmio. It happens at random, and it's due in part to faulty manufacturing. TL:DR version; there is a screw that is actually just barely making contact with the power button, but what it does it creates a hard-shutoff.
If you're running a desktop, my first thing would be to completely break it down. Get everything out of the case, and put it back together on a hard, preferably wooden flat surface. While you're doing that, make sure to only plug in the bare-minimum; Power Supply, CPU, One stick of RAM, and a graphics card if you need it to get video. I would say forego the hard drive the first time, just to see how long it'll run before shutting itself down. Your computer can be turned on by simply using a screw driver, and connecting the two power-pins that would normally have your power button plugged into. Afterward, plug in your hard drive and try to run normally, and see what happens. If it doesn't shut down after the longest you've known it to, then start adding more of your hardware to the machine. Any extra RAM sticks, hard drives, etc. If nothing happens, get it all back into your case and try again. If it happens, then it's either a power button fault, or perhaps a contact on your case yourself that's being made with your main board.
If it's a laptop, well, I can't really say much about it unless it's a Toshiba brand laptop. Hope this helps to some degree.