Harper Lee's novel To Kill a Mockingbird takes place during 1933-1935 in the town of Maycomb, Alabama. The town was fictional, but the novel's depictions of the relations between blacks and whites at the time are quite accurate.
It was not long after the Civil War that the South established "Jim Crow Laws" (1877-1954). These laws were designed to keep the races separate in all aspects of society. "Colored" people had separate, and usually inferior, water fountains, swimming pools, restaurants, churches, and schools. Interracial marriage was forbidden in most places.
It is important to understand this background when thinking about the reasons why Mayella falsely accused Tom Robinson of raping her in the novel.
During Mayella's testimony, contained in chapter eighteen of the novel, Atticus Finch is able to point out to the court the inaccuracies of her testimonies. He asks Tom to stand up, and the court sees that he is crippled on the side that Mayella claims he attacked her from. His arm was mangled in a cotton gin.
Atticus, catching her in the lies of her testimony, gives her a chance to recant, but she refuses. She answers him:
"I got somethin' to say and then I ain't gonna say no more. That nigger yonder took advantage of me an' if you all fine fancy gentlemen don't wanta do nothin' about it then you're all stinkin' cowards, stinkin' cowards, the lot of you."
In chapter nineteen, Tom Robinson testifies to the truth of what happened between him and Mayella. He passed by her place every day on his way to work. He agreed that she had asked him to chop up a chiffarobe, just as she had testified, but that was all that happened that day. Mayella called him over often to do work for her, which he always did without expectation of payment. Tom Robinson testified that on the day in question, Mayella said she had work for him to do inside the house. While he was in the house, she grabbed him and tried to kiss him. Mayella's father saw this through the window and called her a whore. Being caught as she was, it was a natural way out of trouble for her to lie and say that Tom Robinson attacked her.
In addition to the fact that it was not socially acceptable for races to mix, and Mayella's desire to avoid trouble with her abusive father, Scout identifies some other reasons for Mayella's deception. In chapter nineteen, Scout analyzes that she must have been the loneliest person in the world. She was not accepted by whites because her family was considered trash, and she would not have been accepted by black people because of the views at the time. Scout supposes that Tom Robinson was the only person who had ever been decent to her.
The trial of Tom Robinson shows the attitudes of people in that time period toward black people. Even with all the problems and obvious character flaws of the Ewells, people were still more willing to believe their lies than the truth that Tom, a married man with three children, had been propositioned by a white girl.
Mayella lies to save herself from her father's wrath and perpetuates the lie out of her deeply held societal beliefs that she is a higher class of person than Tom. It is also possible that she accuses Tom for another reason: spite. Since Tom spurned her advances, she exacts revenge on him.