The codomain of a function f: A → B is the set B — the collection of possible outputs. Not every element of the codomain need actually appear as an output; those that do form the image (or range) of f. A function is surjective when its image equals its entire codomain.
The codomain is part of the function’s specification, not a consequence of its rule. The function f(x) = x² viewed as f: ℝ → ℝ has codomain ℝ and is not surjective (negative numbers are not outputs). The same rule viewed as g: ℝ → [0, ∞) has a smaller codomain and is surjective. These are different functions because they have different codomains.
In a category, the codomain of a morphism f: A → B is the target object B. Composition requires the codomain of the first morphism to match the domain of the second: g ∘ f is defined only when the codomain of f equals the domain of g.