The Bible describes God as sending the flood because of the pervasive wickedness and violence on Earth (Genesis 6:5-13). Noah and his family were spared because Noah was righteous and blameless in his generation, and he "walked with God" (Genesis 6:9). God instructed Noah to build an ark and bring his family and pairs of animals to save them from the flood.
The text presents this as an act of both judgment and preservation: judgment against the overwhelming sinfulness of humanity, and preservation of a righteous family and the animal kingdom to allow life to continue after the flood.