Classes in Java serve as templates for objects and allow programmers to define data types for their programs. A class defines fields to store data and methods to operate on that data. Programmers can then create objects from a class, which allocate memory and allow access to fields and calling of methods. Java handles memory management automatically by freeing up space for objects no longer being used.