Template functions and classes in C++ allow functions and classes to operate on multiple data types rather than a single data type. The document discusses how template functions define a type parameter to replace the data type, and template classes define member functions and variables as templates. It provides examples of template functions used in ROS for message passing, including advertise and subscribe, which allow publishing and subscribing to messages of different types.