Why do errors such as typos happen? Why do typos happen?
Why do we sometimes type the same word right, and sometimes wrong?

For the same reason, why does a player of Flappy Bird for instance crash at pipe 138? Why not any of the 137 other pipes, and why not any later?
Short answer
Multiple factors are in play that affect error making. Cognitive load is an important element that increases the chance of making errors. Other factors are the presence of distraction, levels of stress or level of vigilance.   

Background
It's a matter of chance. In psychophysics, the field of research I'm spending most of my time in at the time of this writing, models subject responses with psychometric curves. These curves represent the correct rates as a function of the level of the physical stimulus presented (Fig. 1).

Basically, it shows that at high enough stimulus levels the %correct score increases steadily, up to the point where the response is correct for all the trials. In the figure % seen is plotted, for example in a test where image brightness (gamma correction) is varied on a computer screen and the subject is asked if a stimulus is seen yes or no. The characteristic that is mostly reported in such an experiment is the 50% correct point, i.e., the steepest point in the curve. This means that the subject was correct in 50% of the trials.

Now, back to your question - if one not plots intensity, but task difficulty on the x-axis, a similar plot is obtained. Think, for example, speech recognition in noise - when the signal-to-noise ratio increases, %correct will increase. Or, consider a task where cognitive load is slowly decreased, i.e., the task is made easier and easier - then at a certain point the %correct response will also approach 100% (time constraints). Thus, when you are typing, you're typing a lot of characters per minute as you're in a hurry to meet that deadline. And you're thinking about the problem and the solution of the assignment you're working on behind your laptop (cognitive load), your roommates are talking (distraction), and all the while the cat keeps on nestling herself on top of your keyboard (distraction). You might have been working all-nighters, which increases sleepiness that reduces your cognitive abilities overall. These conditions increase mental load and promote incorrect responses (incorrect key strokes) and hence increase the number of typos you make.     

If your assignment's deadline was only 3 months from now, and you were already well on your way and you've set yourself the goal of writing one sentence per hour, you wouldn't make any typos at all, because you can look at the keyboard and find every single individual key before pressing it. 

It's a matter of cognitive load. Which responses go wrong is all a matter of chance. As Fig. 1 shows, it's a matter of chance that overall results, after many repeats, in a nice psychometric curve. 


Fig.1. Psychometric curve. source: Kalloniatis &amp; Luu C (2005)  

Reference
- Kalloniatis &amp; Luu Principles of Vision. In: Kolb H, Fernandez E, Nelson R, editors. Webvision: The Organization of the Retina and Visual System [2005]. Salt Lake City (UT): University of Utah Health Sciences Center