For the entire novel, Huck has felt like he was doing the wrong thing in helping Jim. When he runs across Jim for the first time on the island (chapter 8), he promises to not tell anyone, even though, he says, if people found out he was helping, they "would call me a low-down Abolitionist and despise me". So he reveals his attitude that helping Jim is wrong-but, he'll do it anyway because he gave his word. Later in chapter 18, when Jim is getting excited about becoming a free man and working to buy his children back, Huck states,
"Here was this nigger, which I had as good as helped run away, coming right out flat-footed and saying he would steal his children-children that belonged to a man I didn't even know; a man that hadn't ever done me no harm."
The irony of this quote is evident; these children belong to Jim, not the man who "owns" them through slavery, but Huck feels the white man has more right to them than Jim. He goes off to turn Jim in, but doesn't, then gives himself a guilt trip for it, "because I knowed very well I had done wrong."
So, in chapter 31 when Huck is deciding whether or not to write a letter to Miss Watson, he has had a good 23 chapters of feeling guilty for helping Jim and not turning him in. He figures that Jim getting sold is
"Providence slapping me in the face and letting me know my wickedness was being watched."
He is worried that God is punishing him; not only that, that "it would get all around that Huck Finn helped a nigger," and
"the more my conscience went to grinding me, the more wicked and low-down and ornery I got to feeling."
So, he writes the letter and directly afterward, "felt good and all washed clean of sin." But, then he thinks of Jim and all of the times that they had been through together, and how Jim considered him a true friend, and all of the kind things Jim had done for him, and in one quick moment, tears up the letter and declares, "All right, then, I'll go to hell." This statement is ironic because no, he won't go to hell, he's probably doing the right thing, but he doesn't think so. He still feels that slavery is the right thing, and helping a slave the wrong thing. History has revealed otherwise, so we know better, but Twain uses this moment to reveal how many people felt that slavery was in the right, and that the abolitionists were in the wrong. His medium is a young Huck who is struggling with his conscience, and Twain uses him to convey how absurd it is to think that helping a man obtain freedom could possibly be wrong.
When Huck found out that the duke and king had sold Jim to a strange family, he felt that he "couldn't see no way out of the trouble." He goes through a long cycle of indecision, not knowing what to do, before he finally settles on his choice. At first, he figures that if Jim was going to be a slave on a strange plantation, "it would be a thousand times better for Jim to be a slave at home where his family was," and so decides to write Miss Watson to tell her where Jim was. Then he vacillates; he doesn't want to because Miss Watson would be mad at Jim for running away, and everyone would know that he himself was "low-down and ornery" for helping a runaway slave. He mulls this over, feeling more and more crummy for doing just that. He feels so guilty for helping Jim that he thinks Jim being sold was "providence" punishing his wicked deeds. He even decides to pray to "try to quit being the kind of boy I was and be better," which is huge, because at the beginning of the book he had decided that prayers were worthless. He finally just writes the letter, but then thinks of all the good times they had had, and how Jim considered Huck such a special friend. Then, he tore the letter up and in an ironic statement about that action, says, "All right then-I'llgo to hell." That statement is ironic because he was doing the right thing, but thought he'd go to hell for it. But he does the right thing in the end, and goes to help Jim.
Those are just some thoughts; I hope that they help to get you started. Good luck!