Without question the easiest way to improve your timing is to play with good musicians. It is the most effective way of learning to keep up with the tempo of the song, and it teaches you about rhythm and how rhythm is established.
The drummer, bass player and rhythm guitar player create rhythms by playing different parts, not by playing on the same beat. This creates a complex and interesting "composite" rhythm and it is critical that each player have a good sense of timing and a feel for what other members of the band are playing. Any band that have put in a lot of practice and have gotten their timing right will sound "tight" and effective.
However, it is not always possible to play with other players either by choice or by circumstance. This is when developing a sense of timing becomes more difficult. Varied tempo is sometimes done on purpose for effect in songs, however, as a beginner, you should focus on learning regular and steady tempos. There can be a very real danger in losing your timing when it comes to a tough chord change or a difficult run.
To remedy this, it is recommended to employ a metronome or drum machine. These are a must have for the practice tool box, because it reminds you when you go out of time and keeps a steady beat to keep you in line.
The simplest and cheapest of these "time-keepers", the metronomes are, in my opinion the best solution because they are simple to use and travel well, in case your practice space varies. Most metronomes operate on a battery and plug directly in to your amplifier to produce "standard" rock rhythms.
The more expensive drum machines can provide programmable, complex beats, and usually require hours of study to get to understand them, although the end result is often worth it.
The other alternative is to just use the old fashioned "play along with the cd or mp3." Which was called a cassette tape in my day.
Like anything else when it comes to your guitar playing, it takes time and practice to master having great timing. Stick at it and practice good habits!
I hope this helps.