Ralph joins the dance because he gives in to the primal instincts to belong and to kill.
The dance evolves slowly over the course of the book. In chapter 4, when the boys first begin their war dance, Ralph’s reaction is telling. He is both jealous and appalled.
Ralph watched them, envious and resentful. Not till they flagged and the chant died away, did he speak. (ch 4)
Ralph’s attempt to call a meeting is a last-ditch effort to bring the boys back to civilization as they are about to descend into chaos. Ralph feels that as their leader he must do this, but he does not know how.
He lost himself in a maze of thoughts that were rendered vague by his lack of words to express them. (ch 5)
Ralph struggles to be wise. He is looking for a way to bring order to the group, because none of their attempts have worked.
During the pig-killing dance, Ralph feels tempted, and the “desire to squeeze and hurt was over-mastering” (ch 7). By the time Jack has implanted himself in Castle Rock and no longer recognizes Ralph’s authority, Ralph and Piggy have a difficult choice to make. Jack’s group is the hunters, so they have the meat. They have the fun. Ralph cannot compete with that.
Piggy and Ralph, under the threat of the sky, found themselves eager to take a place in this demented but partly secure society. (ch 9)
By joining the dance, they are facing their fears. The ritual “hemmed in the terror and made it governable” (ch 9) and allowed them to be part of a group again.
The primal instinct to remain with the group, and to kill, was enough to overwhelm them in the moment. It gave them something to do to get away from the hopeless situation they were in. They knew it was wrong, but the equivalent of massive peer pressure was enough to make them join in anyway.
The descent into savagery and the influence of "the beast" is very prevalent at this point in the story. Simon's realization that the beast is not real at all (Chapter 9)(although he never did believe it really)and that the boys must be told is about to change everything. The storm gathering overhead foreshadows the upcoming events.
Ralph and Piggy have managed to retain their distance from Jack and his "tribe" and Jack's increasing influence over the group of boys. Jack is still angry that he was not voted leader and Ralph was and his invitation to join him for roast pig is very inviting - even to Piggy. Piggy rationalizes that they can eat and then return to their own area and no harm will be done. He suggests it would be a good idea, "I mean—to make sure nothing happens.”
It is unusual for Piggy to take risks but in this instance, it is Ralph who recognizes the real danger of joining Jack for any kind of activity.
As Ralph's civilized world disintegrates, Jack's savage society becomes more distinct and powerful.
Ralph and Piggy remain guarded when they do get to Jack and, inevitably, Ralph and Jack argue about control. It is significant that there is no shelter at Jack's camp and it is about to rain.
When the littleuns start to panic due to the storm, Ralph and Piggy - also a little bit concerned and even intimidated, join the dance. It is a distraction but will serve far more than to simply distract them as Simon wanders, unwittingly, into their, by now, ritualistic dance.