Dick was once a very common nickname for people with the given names of Richard or Ricard. In 1890, for reasons unknown, Dick started to be used as a slang term for penis, and the use of Dick as a nickname thereafter became less and less common, although it is still used today, perhaps primarily in rather informal contexts. Some well known people still go by Dick -- Dick Armey and Dick Cheney, for example.
Dick first appeared in the 1220s, as a rhyming nickname for Rick, which is in turn the shortened form of Richard. Rhyming nicknames were common in those days, and the centuries from 1200 to 1600 produced hundreds of them. We still use many of them, such as Polly from Molly (which in turn comes from Mary), Bill from Will (from William), and Bob from Rob (from Robert).1 These were often used as a diminutive form to begin with, but later became more common and formalized, to the extent that we have surnames like Dickson, Dixon, and Dickens, all meaning the 'the son of Dick'.
By the 1550s Dick had come to mean everyman, used to refer to any random guy, as in 'hey, look at that fellow over there!'. At this point it had none of the negative connotations that we would assume if we heard someone speaking about 'that dick over there'. It was still a diminutive, so it was sometimes used as a synonym for 'lad', although it was also used for older men. This is the sense that it is still used in in the phrase Every Tom, Dick, and Harry.
From the 1500s through the 1800s 'dick' saw an explosion of meanings, most of which are no longer used. Quoting from the OED, dick was used to mean: 1. a kind of hard cheese eaten in Suffolk; 2. a plain pudding, as in treacle dick, groaty dick, and spotted dick; 3 a riding whip (as in 'he held a gold-headed dick'); 4 a leather apron; 5 a ditch or the bank of a ditch (perhaps a form of dike); 6 an abbreviation for dictionary, and thus a synecdoche for long and fancy words ("a man who uses fine words without much judgement is said to have 'swallowed the dick'"2); 7 an abbreviation for declaration, which in the late 1800s was not uncommon in the phrase 'to take one's dick' -- meaning to make one's declaration2; 8. and an abbreviation for detective, as in 'a private dick'. There are also some odd regional constructions, such as the dick-a-dilver, meaning a periwinkle; the dick-a-Tuesday, meaning a will-o'-the-wisp; the long-tailed-dick, meaning the long-tailed titmouse; and the dick-ass, meaning a donkey.
We do not know how dick came to have its current slang meaning, except that it originated in the ranks of the British army as a slang term among the troops, circa 1891. This suggests that it arose simply out of tradition, as the related slang term Johnson came to us from the same source, circa 1863. As did the also related term John Thomas (which never really made it across the Atlantic), in 18873.
Footnotes:
1. Richard also gave rise to the nickname Hick. Eventually hick came to mean a random man-on-the-street, and then an innkeeper or hackneyman (i.e., someone for rent) and by the 1700s it referred to any provincial, unworldly person (a rube). We still use it in this sense today. John, Jack, and Tom, along with Richard and Dick also came to have similar meanings in various contexts.
2. Yes, really.
3. It is interesting, although completely off-topic, to note that the earliest of these British army terms that I can find is in the song Yankee Doodle, which was written in 1775 by British Army surgeon Dr. Richard Schuckburgh, and was sung to mock the American troops. It is suspected that 'doodle' was intended to be a cunning yet subtle reference to the male member, but that this meaning was lost when the Americans decided to take the song as their own.