How do the boys almost die or get hurt at the end of the story?
If you are referring to the very end, which I believe you are, it is not two boys, rather brother (Jem) and sister (Scout) who get hurt at the end of the novel.
After the school pageant, Jem and Scout are walking home through the woods when they are suddenly attacked. Mr. Ewell grabs a hold of Scout, who is still in her ham hock costume, and pushes her to the ground. In an effort to save Scout, Jem attacks Mr. Ewell. Bob Ewell throws Jem to the ground, breaking his arm, thus brings us to the beginning of the novel where Scout tells the story.
During the scuffle, Boo Radley appears and begins to fight with Bob Ewell, who has a knife. During this struggle, Mr. Ewell is stabbed with his own knife. Boo then carries Jem home as Scout runs after them.
I don't think you mean "the boys" here, exactly. Scout (a girl) and Jem (a boy) are both on the run from Bob Ewell, who intends to bodily harm both of them out of vindictiveness prompted by the Tom Robinson trial. Scout is saved by her ham costume, but Jem winds up with a broken arm in the struggle. Both children are saved when Boo Radley intervenes and stabs Bob Ewell under the ribcage with a kitchen knife so that the children will not be harmed.
The law turns a blind eye to the event, as Sheriff Heck Tate tells Atticus to "let the dead bury the dead." This scene is one of the most climactic in the story itself.