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 numbers in the decimal expansion of pi (3.14159265...). Unfortunately, whether the digits of pi 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. (In fact, when the monkey-typing test was actually carried out by researchers, what resulted was "five pages consisting largely of the letter 'S'." Apparently, monkeys don't tend type randomly.)
A more important problem involves the "infinite amount of time" involved in 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 all have finite lifetimes. Even given an infinite supply of monkeys, typewriters, observers, typewriter ribbon, and paper, there's no guarantee that the universe will last long enough to get the result. In fact, even a short text like the first sentence of this paragraph could take an extremely long time to generate randomly.