Why does an "obviously good guy" (Hagrid) retaliate against a child for the child's father's misbehavior (see here for detailed quotes)?
First of all, other answers are contradicted by book canon (they rely on the fact invented for the movie). In the book, Dudley did NOT eat Harry's cake, so that was NOT the reason at all. The opposite is true: Vernon warned Dudley to NOT touch any of Hagrid's food:
... sausages from the poker, Dudley fidgeted a little. Uncle Vernon said sharply, "Don't touch anything he gives you, Dudley."
The giant chuckled darkly.
"Yer great puddin' of a son don' need fattenin' anymore, Dursley, don' worry.
They probably added it to the movie exactly to avoid the moral ambiguity your question noted.
Second, to asnswer your question:
While Hagrid was a "good guy" in general, he was VERY VERY impulsive and not exactly prone to rational decisions. Probably a result of being a half-giant. Remember, this is a guy who decided on a lark to grow a dragon from an illegal egg. Or to grow a deadly agromantula in a castle. Or to bring his giant half-brother into the forest - AND ask Hermione to look after him. Or get into a shouting match over being allowed into the forest with armed Centaurs.
He was severely provoked throughout that WHOLE conversation. He found out that
Harry didn't know about Hogwarts
Harry didn't know about being a wizard
Harry didn't know about his parents AND heard insults about them from Dursleys
Harry didn't know about being famous and The Boy Who Lived
Vernon said that Harry won't go to Hogwarts.
Then to top it off, Vernon insulted Dumbledore, who was held in extreme esteem by Hagrid (that was partly due to making sure Hagrid wasn't arrested AND was allowed to work at Hogwarts).
So, when that happened, Hagrid basically blew up and lost his temper.
"Shouldn'ta lost me temper," he said ruefully, "but it didn't work anyway. Meant ter turn him into a pig, but I suppose he was so much like a pig anyway there wasn't much left ter do."
Why he specifically hexed Dudley isn't clear in canon - but he wasn't exactly thinking straight, as he forgot about not being allowed to do magic!
BUT!!! ... there's a circumstantial evidence that JKR actually did it on purpose, and NOT to showcase Hagrid's character at all - instead, to provide character development for Dudley!
A question also surfaced surrounding Harry Potter’s non-magical relatives, the Muggles who have always tortured or mistreated Harry, because of their fear of magic. For revenge, Harry has magically tortured his cousin Dudley. 'I like torturing them,' said Rowling. 'You should keep an eye on Dudley. It’s probably too late for Aunt Petunia and Uncle Vernon. I feel sorry for Dudley. I might joke about him, but I feel truly sorry for him because I see him as just as abused as Harry. Though, in possibly a less obvious way. What they are doing to him is inept, really. I think children recognize that. Poor Dudley. He’s not being prepared for the world at all, in any reasonable or compassionate way, so I feel sorry for him. But there’s something funny about him, also. The pig’s tail was irresistible.'
Now, there's no exact proof, but to me it sounds like the pig tail was basically a precursor in life lessons (don't be a pig! -Circe. Yes, I'm making that parallel on purpose) that ended up with Dementor attack which majorly straightened Dudley out.
Dudley was sneakily eating Harry's cake and that, with already being upset, was why Hagrid lashed out at the child. The Dursleys on the whole seem like pretty mean and petty people, especially in year one before they learned to fear consequences for their actions. Like getting a tail or being blown-up like a balloon (à la Aunt Marge.)
WARNING: This answer is from the movie, but is contradicted by book canon. In the book, there's no mention of Dudley eating that cake - and the opposite is true; Vernon tells Dudley not to eat ANYTHING that Hagrid brought (e.g. sausages).
It is indeed a fact in the movie - the canon (book vs movie) was not specified in the question. I should have inferred it. Anyone is free to downvote if they disagree, obviously. Or, just upvote DVK. I like his answer better myself. -meat™**
Hagrid is a nice guy until you anger him. He didn't give Dudley the pig tail because of what Vernon had said, though. He gave Dudley the tail because he was helping himself to Harry's birthday cake that Hagrid had brought only for Harry. Hagrid also explained later that he had meant to turn Dudley completely into a pig, but only the tail appeared because Dudley already was enough like a pig that the spell didn't work fully.
WARNING: This answer is incorrect. In the book, there's no mention of Dudley eating that cake - and the opposite is true; Vernon tells Dudley not to eat ANYTHING that Hagrid brought (e.g. sausages).
I think there has been a revision to the book. When I first read it, Dudley got a tail for eating Harry's cake. In the version I am reading now there is no mention of it. Probably some PC bull about sparing the feelings of fat children.