Infinite monkey theorem
The infinite monkey theorem is a result in probability theory stating (informally) that any event that is at all possible must occur eventually, as long as one waits long enough. The name of the result is based on the popular notion that "a monkey typing randomly on a typewriter forever will eventually produce the complete works of William Shakespeare".
To state the theorem more formally, one must consider an unending sequence of random events (the pressing of random keys in this case), each of which has some chance of matching any of a set of possible outcomes (the letters of the English alphabet along with punctuation and various forms of whitespace — note that for "random" typing, all individual keys are equally likely to be pressed, but this is not strictly required, as long as each key has some fixed, non-zero probability of being chosen at each step). The theorem then states that the probability that a specific sequence of interest (the string of characters comprising the works of Shakespeare) will appear somewhere in the unending sequence of random events approaches one (certain to happen) as the length of random events grows to infinity.
In fact, in such an infinite sequence of events, any specified sequence must occur an infinite number of times!
Arbitrary events over time
This framework can be extended to any kind of event occurring over an infinite stretch of time. By considering as the sequence of random events the occurrence or non occurrence of the desired outcome in different periods of time (say, every second, or minute, or day, etc.), we can say that as long as there is some positive probability, no matter how small, that the event could happen in any given time period, then it will (almost surely) happen eventually, given an infinite amount of time to wait.
When viewed from this perspective, the complete works of Shakespeare must happen eventually (with probability one) simply because it is possible to intentionally type such a text given an adequate amount of time (say, a year). Thus, random typing over the same time period must also be able to produce the same result with some positive probability. (To see why this is so, consider that every possible collection of "Shakespearean-length" strings of characters would be equally likely in random typing, and one of those strings matches the works of Shakespeare, so the probability is one divided by the number of such collections of characters — a large denominator, to be sure, but not infinite and thus the probability is not zero.) The result then follows from the fact that an infinite amount of time can be divided up into an infinite number of such time periods (years, in this case).
Note that another setting involving a similar idea is looking for specific sequences of digits in the decimal expansion of π (3.14159265...). Unfortunately, whether the digits of π behave (in ways that are relevant to this discussion) like a random sequence of digits is currently an open question in mathematics.
Applying the theorem in the real world
There are some problems with applying this theorem in the real world. In the original formulation above, although the individual keys do not have to be equally likely, the probability for each must be fixed throughout all time. This restriction can be relaxed somewhat, but if it is not satisfied then it is possible that the probability of generating a specific text would not be one, and thus not be "guaranteed" to happen (in particular, this would happen if the probability of generating a certain character in the target text decreases over time sufficiently quickly).
Similarly, in the second formulation (a target event occurring or not occurring in each of a sequence of time periods) the probability of the desired event must be the same for all time periods of the same length. This may not be correct, particularly in real-world applications.
When the monkey-typing test was actually carried out in 2003 by lecturers and students at the University of Plymouth, the five pages of resulting text "consisted mainly of the letter 'S'", although "towards the end... the letters A, J, L and M also [appeared]." Apparently, monkeys don't tend to type randomly and consistently over time.
A more important problem, however, involves the "infinite amount of time" required by the theorem. It is assumed that one can actually wait as long as necessary to generate the desired result. In reality, however, the monkey, the typewriter, and the observer waiting on the output all have finite lifetimes. Even given an unending supply of ideal (randomly typing) monkeys, typewriters, media (i.e., ribbon and paper, or computer monitors), and observers, there's no guarantee that the universe will last long enough to get the result.
In fact, even a short text like "the quick brown fox jumps over the lazy dog" could take a ridiculously long time to generate randomly. Assuming a keyboard containing only 26 letters plus a space character, even if the monkey could type one thousand characters per second — faster than any human typist — you could expect to have to wait many, many orders of magnitude longer the current age of the universe before you would get a string of 43 characters that matches that phrase. Obviously, any keyboard that could generate Shakespeare would have to contain more than 27 keys (i.e., if punctuation is taken into account), making the task take even longer. Finally, extrapolating this result to longer texts, like Shakespeare, means wait times even more orders of magnitude longer.