Briefly, a festival is a gathering of people to celebrate something, historically religious holidays (feast days), but now also secular ones, e.g. Kwanzaa, or the Burning Man, initially held to celebrate the summer solstice. Mardi Gras is a famous New Orleans festival, a religious one celebrated more quietly in other areas.
A fair is a gathering of vendors or tradespeople for the entertainment and/or commerce/other purpose). There are job fairs (hardly a festival), book fairs, farm shows (also called fairs) where people show their prized livestock or farm products for the entertainment (and often sales for charity) of both fellow farmers and non-farmers alike, county fairs (mostly just summer entertainment), etc.