Time flies like an arrow; fruit flies like a banana

"Time flies like an arrow; fruit flies like a banana" is a humorous saying that is used in linguistics as an example of a garden path sentence or syntactic ambiguity, and in word play as an example of punning, double entendre, and antanaclasis.
Analysis of the basic ambiguities.
The point of the example is that the correct parsing of the second sentence, "fruit flies like a banana", is not the one that the reader starts to build, by assuming that "fruit" is a noun (the subject), "flies" is the main verb, and "like" as a preposition. The reader only discovers that the parsing is incorrect when it gets to the "banana". At that point, in order to make sense of the sentence, the reader is forced to re-parse it, with "flies" as the subject, "fruit" as a qualifier of "flies", and "like" as the main verb. Additionally, the insects known as "fruit flies" do indeed enjoy bananas, adding yet another valid reading of the sentence. The only minor flaw could be "a banana", which combined with "fruit flies" without article, makes it sound like every fruit flies of the world have a taste for a specific specimen of banana in the middle of tons of this fruit existing on Earth. The correct wording should rather be "fruit flies like bananas (in general)". Still, the previous sentence is grammatically correct.
The role of the first sentence is to predispose the reader towards the incorrect parsing of the second one. However, after re-parsing the second sentence, the reader presumably becomes aware that the first sentence too could be re-parsed in the same way.
The sentence "time flies like an arrow" is in fact often used to illustrate syntactic ambiguity. 
Modern English speakers understand the sentence to unambiguously mean "Time passes fast, as fast as an arrow travels". But the sentence is syntactically ambiguous and alternatively could be interpreted as meaning, for example:
In addition, the sentence contains semantic ambiguity. For instance, the noun phrase "Time flies" could refer to all time flies or particular time flies, and "an arrow" to all arrows, a particular arrow, or different arrows for different flies; compare "Fruit flies like a banana", "Fruit flies ate a banana", "Fruit flies live on a banana". Moreover, "Time flies" could refer to "flies of the "Time" magazine", or "flies of the Pink Floyd song "Time"". Indeed, a copy of the magazine or the song could also be the subject doing the flying. Furthermore, "like" as a verb could either signify general enjoyment, or the usage of a like button.
History.
The expression is based on the proverb: "Time flies", a translation of the Latin "Tempus fugit", where "fly" is to be taken in the sense of "flee".
An early example of a pun with the expression "Time flies" may be found in a 1930 issue of "Boys' Life":
Anthony Oettinger gives "fruit flies like bananas" as contrasted with "time flies like an arrow" as an example of the difficulty of handling ambiguous syntactic structures as early as 1963, although his formal publications with Susumu Kuno do not use that example. This is quoted by later authors.
A fuller exposition with the banana example appeared in a 1966 article by Oettinger.
This article prompted the following response in a letter:
Time Flies Like an Arrow
"An Ode to Oettinger"
<poem>
Now, thin fruit flies like thunderstorms,
And thin farm boys like farm girls narrow;
And tax firm men like fat tax forms –
But time flies like an arrow.
When tax forms tax all firm men's souls,
While farm girls slim their boyfriends' flanks;
That's when the murd'rous thunder rolls –
And thins the fruit flies ranks.
Like tossed bananas in the skies,
The thin fruit flies like common yarrow;
Then's the time to time the time flies –
Like the time flies like an arrow.</poem>
— Edison B. Schroeder (1966)
The verse is popular as a specimen of didactic humor trading on syntactic ambiguity. Like the poem "The Chaos", by Gerard Nolst Trenité, its themes are popular among practitioners and students in fields such as natural language processing and linguistics.
Other attributions.
The saying is sometimes attributed to Groucho Marx, but the earliest attribution to him dates only to 1982, and there is no reason to believe he actually said it.
Use in linguistics.
The saying is used as a linguistic example of antanaclasis, the stylistic trope of repeating a single word, but with a different meaning each time.
It is also used as an example of punning. The wordplay is based on the distinct meanings of the two occurrences of the word "flies" (the verb "travel through the air" and the noun for certain insects), and of the word "like" (the preposition "similarly to" and the verb "enjoy"). For example, the second clause can be read as "fruit travels through the air similar to a banana" or as "certain insects enjoy a banana".
This is an example of a garden-path sentence, a phrase that the reader or listener normally begins to parse according to one grammatical structure, and is then forced to back up and reparse when the sentence ends in an unexpected way.