Codeunit is a container for C/AL code that you can use across different application objects. -- just think of it as a class of functions.
Codeunit by default contains two functions: Documentation and OnRun
Documentation: where you write descriptive information about the codeunit
OnRun: It executes any code when you run the codeunit.
By using codeunit, you eliminate the need for duplicate code and make code easier to maintain. You can access the codeunit's function from another codeunit by declaring a datatype of codeunit in a variable and specifying the name of the codeunit in the subtype. Then you can do this
<CodeunitVariableName>.<FuntionName>. To access a codeunit from other application objects, you must set the Local Property of the function to "No"