IMO, it really just takes lots and lots of practice. :o Perhaps try playing through a whole bunch of pieces that you know, with some sort of backing track or metronome or drum beat? And keep doing this a little bit every day until you get it.
If you don't realise when you're out of time, try...