It’s a pretty straightforward answer actually: one of those cases where when you see the steps between A and F, it makes sense.
Back in the 12th and 13th centuries, due to the need to handwrite everything, short versions of names were quite common, to save time writing. Richard was therefore often shortened to Rich, Ric, or Rick. Also at the time, rhyming nicknames were quite common, so Richard often became Dick or Hick (see also: Bob and Bill). Hick never really stood the test of time, but obviously there was something appealing about Dick.
Not only did it become a common nickname for Richards everywhere, but for a long time it was a general term for the average man, much as Jack was later. Early evidence of how commonplace it was can be found in Shakespeare’s Henry IV, Part I:
I am sworn brother to a leash of Drawers, and can call them by their names, as Tom, Dicke, and Francis.
This particular line may be the origin of the phrase any/every Tom, Dick, and Harry.
Nowadays of course, the name’s not so common, largely due to it’s cheekier meanings. The first recorded use of dick to mean penis is from the British Army in the 1890s, though it had probably been in use in speech for some time beforehand. The name was still quite common well into the 1950s, though once censorship began to loosen up towards the end of the century, and we became more used to hearing dick as a slang term, it waned in popularity.
The curious thing is why dick came to mean penis. I think it’s because Dick was so emblematic of the average man, that it seemed fitting to name every man’s little man dick, perhaps similar to how Johnson is used in that way in American English, as a deliberately ordinary surname.
There are still a few high-profile Dicks out there (Dick Cheney, Dick Van Dyke, Donald Trump), but I think that in the next 20 years or so it won’t really exist as a name. That happens with names anyway, as they’re quite prone to the whims of fashion (how many Khaleesis and Kylos were there 10 years ago?) It’s just a shame for all the Dicks of the world that their name became a slang term because it was such a popular name in the first place. I guess that’s why it’s never good to become too popular.