Untangling how, when and why we came to say, "Here's my two cents" when offering an opinion is a bit of a task in that it has to be done in three parts. First, our language contains many specific terms for goods or services that cost two cents (or twopenny, two-pence or tuppence), some of them quite old. Second, over time "two cent" or "twopenny" also became descriptors of items or concepts not worth much, if anything. Finally, that modifier became attached to the practice of offering unsolicited advice.
Over the course of our recorded history, many goods or services have borne a set price of two of a civilization's smallest unit of currency, such as twopenny ale, twopenny post, even twopenny rope (an archaic idiom for a flop house). Sometimes that amount was even the sum paid by someone on a particularly memorable occasion, such as "The widow's mite" of Mark 12:41-44 fame wherein the widow donated two mites (the least valuable coin there was) to the temple, yet, by doing so, gave all she had, in contrast with far more prosperous donors who proffered only a small portion of their wealth.
The use of "two cent" and "twopenny" as dismissive modifiers indicating items under discussion are of incredibly small value, rather than are priced that actual amount, dates to 1560 (e.g. "To make the people thinke that we reade nothyng els but ij. penny doctoures, as ye cal them" , "Even in a two-peny matter" , "He cares not two-pence for the land-tax bill" , "This woman, with her twopenny gentility" , "The date for Bissolo's death, for which I don't care just now a tuppenny damn" ).
"Here's my two cents" in the sense of airing an unsolicited opinion dates only to the mid-1920s, and likely began in the U.S. It appears to be an extension of the usage noted above, in that the "two cents" is employed as a way of saying "Here's my little bit of nothing."
As to how this particular idiom came to be, no one knows. Some have guessed that since, at one time, it cost two cents to mail a letter, one incurred an actual cost of two cents to give someone advice or let them know your opinion. Others have postulated that the ante in a poker game was two cents, therefore one would "ante in" (enter a conversation) by throwing in one's metaphorical two cents. While the gambling explanation is attractive, nothing supports it.
But why do we say, "Here's my two cents on the matter" instead of "Here's what I think"? Why do we need to couch such an addition to a conversation in linguistic smoke?
We do so in an effort to lessen the impact of a social trespass. By identifying one's take on a matter as being worth no more than a pittance, some of the social crime of butting in unasked is undone — the analysis or advice is offered in a self-deprecatory "Well, this likely isn't worth all that much, but here it is anyway" fashion. The one who couldn't keep his thoughts to himself, while still inserting himself presumptuously into something that wasn't his business, is at least being humble about it.