The shore is a place of danger for Jim and Huck. In the beginning of the novel, it represents a lack of freedom. Jim is literally a slave whereas Huck feels imprisoned in the civilized home of the Widow Douglas, and later he is kidnapped and literally imprisoned by his...
father as well as subjected to beatings from him. Both Jim and Huck need to escape the frightening and imprisoning life they experience on the shore.
Even after they escape to the raft, going to the shore—or off the raft—continues to be dangerous for them. For example, when Huck disguises himself as a girl and heads to land to find out what is going on in the world, he learns that there's a high price on Jim's head and that the bounty hunters are after him. Huck has to rush back to the raft so that the two of them can hurry up the river and away from danger. Going on the shore also, for example, means running into the dangerous and bloody feud between the Shepherdsons and Grangerfords, where gracious living is simply a veneer placed over barbarous behavior.
The raft, however, represents freedom and safety. As Huck notes in chapter 29,
in two seconds away we went a-sliding down the river, and it did seem so good to be free again and all by ourselves on the big river, and nobody to bother us.
The raft is the place where Huck and Jim can shed their clothes, which represent their defenses, and get to know each other not as white master and black slave but as two human beings. They don't have to conform to social norms as they drift in the middle of the river. As Huck puts it, the raft could
float wherever the current wanted her to; then we lit the pipes, and dangled our legs in the water, and talked about all kinds of things—we was always naked, day and night, whenever the mosquitoes would let us.
As they float on the raft, Huck and Jim experience the peace and beauty of nature. On the raft, too, Huck develops a moral compass that allows him to escape the false morality he has learned on shore. Although he has been taught all his life that it is a sin to help a slave to escape to freedom, on the raft, he learns to evaluate ethical issues for himself. On the raft, he learns to accept Jim's full humanity, and he decides not to betray him even if it means he will go the hell.