The men come to the jail one evening because Tom Robinson has been moved there. He was kept in a neighboring jail, and is moved to the Maycomb jail to await the start of his trial. Heck Tate, the sheriff, and other men are concerned about Tom being in the Maycomb jail. They come to the Finch house to express their concern to Atticus--that someone might try to get to Tom and kill him (lynch him) before the trial. This is why Atticus stations himself outside the jail that night.
The group of drunk farmers, including Mr. Cunningham, show up in order to get to Tom and, presumably, kill him before the trial. In this time, people of color were looked down upon, and some white people, like those in the mob, may wish to harm Tom just because he's black or harm him because they assume he's guilty, because he's black.
Atticus explains the next day to Scout that, even though Mr. Walter Cunningham was part of the mob, they weren't to stop being friends with him or his son. Atticus believes that the mob was just made up of men, and all men have their blind spots. With enough alcohol and peer pressure, even good men can be persuaded to join mobs and do things they wouldn't normally do under their own will.