A hash function is a mathematical algorithm that converts an input (or 'message') into a fixed-size string of bytes, typically a hash code. It is widely used in computer science for data indexing, retrieval, and encryption, ensuring data integrity and security.