Get a metronome, or better still something that can play you drum patterns. Some multi-fx pedals have some preset drum patterns. Used drum machines can be bought fairly cheaply.
A couple of advantages of a drum pattern over a metronome click:
- You can pick a pattern that fits the piece you're practising (on the down side, you can pick a pattern that's a really bad fit for what you're practising)
- A bar has more structure. It's more obvious when you're out by a beat.
- It's more similar to when you play with a band (assuming you're playing in a genre that uses drums)
Of course, you can make a drum machine play a simple metronome pattern too.
Your problem is that you have to divide your attention between your own playing, and the beat. You're giving too much attention to your playing, and not enough to the beat.
So, learn to reverse the situation -- overcompensate and give too much attention to the beat and not enough to your music. Then find the perfect medium position between the two.
Turn the beat up loud, and as you play, listen to the beat more than you listen to yourself.
Once you're comfortable with that, bring things more into balance, so that you can hear yourself and the beat equally. But still concentrate on listening to the beat more than your own playing. If you fluff a note, play on. If you have skip part of a passage to get back in sync with the beat, do it. Resynching with the band is a good skill.
Keep practising in this way. When you think you're playing in time, you can readjust your priorities, and return to paying attention to your part.