As a common synonym for a penis it dates to some time in the late 1800's according to A Dictionary of Slang and Unconventional English and came about, perhaps unsurprisingly, in the military.
https://www.grammarphobia.com/blog/2011/01/dick.html has a good piece on the term and its possible origins from a similar term in Chaucer, dighte, which was used to mean copulation.
As a general insult depicting a person of poor character it is far older and apparently appears in works like The English Rogue (1674): https://archive.org/details/englishroguedes00kirkgoog p.64-65
The next Dick I pickt up for her was a man of a colour as contrary to the former, as light is to darkness, being swarthy; whose hair was as black as a sloe; middle statur'd, well set, both strong and active, a man so universally tryed, and so fruitfully successful, that there was hardly any female within ten miles gotten with child in hugger-mugger, but he was more than suspected to be Father of all the legitimate.
It usage as an insult may simply come from the similarity to another word (perhaps that same one from Chaucer) or from a variant of its use in a region; Dick may have been a general term similar to how some modern Americans might use "John" or how older people here in Scotland will use "Jim" or "Jimmy" to refer to an unknown person.
Phrases like "Tom, Dick or Harry" meaning unknown or uninteresting people date back at least as far with Shakespeare's Henry IV, Part 1 (1597) containing the line:
I am sworn brother to a leash of Drawers, and can call them by their names, as Tom, Dicke, and Francis.
Unfortunately I don't know of, and cannot find, anything more definite than this.
As a related question, how did the word dick become associated with private investigators (i.e. private dick)?