By being near someone who is/has been infected recently. Also, Gastroenteritis can be caught by drinking unclean water and not washing your hands after meals and going to the toilet.
Gastroenteritis can be avoided by practicing good hygiene, which includes washing hands thoroughly after using the bathroom or coming in contact with an infected person