Overview
mktemp creates a new, empty file or directory with a name nobody else is using and prints the name. Scripts use it for scratch space, because a fixed name such as /tmp/data can collide with another run or be guessed by someone else. You give a template ending in XXXXXX, and mktemp replaces the Xs with random characters.
Background
A mktemp utility appeared in OpenBSD 2.1, and the FreeBSD one was written independently from the OpenBSD manual and first appeared in FreeBSD 2.2.7, according to the macOS manual. Today the name covers GNU mktemp, the mktemp that macOS ships, and the mktemp applet in BusyBox. They agree on a template and on -d; the default location and the extra options differ.
Examples
Create a temporary file and keep its name in a variable:
f=$(mktemp) && test -f "$f" && echo created; rm -f "$f"
Create a temporary directory with -d:
d=$(mktemp -d) && test -d "$d" && echo created; rmdir "$d"
Choose where the file goes and how it starts, with a template that ends in six Xs:
f=$(mktemp cache/item.XXXXXX) && ls cache | grep -c .
Build a name from a template in the current folder:
f=$(mktemp work.XXXXXX) && echo "${f%.*}"
Options
-d
Create a directory instead of a file.
Common mistakes
Do not make up a name such as /tmp/myapp.$$: it can be guessed, and mktemp exists to avoid that. A file made by mktemp is not removed for you, so delete it when you are done, or use trap 'rm -f "$f"' EXIT in a script. On macOS the template's Xs must be at the very end, or they are not replaced. mktemp makes the file with owner-only permissions, so another user cannot read it.
Portability
Only the template and -d are in the Common examples, and the default folder is left to the system: usually /tmp or the folder in TMPDIR, with details that differ. GNU's -p and --suffix, and BSD's -t, are not portable. The Common examples were executed on GNU coreutils 9.11, macOS system tools, and BusyBox 1.37.0 (see the development guide for the exact check).
About macOS mktemp
This is the mktemp in /usr/bin on macOS. It follows the BSD implementation, where -t takes a prefix and creates the file in the user's temporary folder.
Apple notes
macOS mktemp has -t PREFIX, which makes a name in the folder from TMPDIR. Plain mktemp ignores TMPDIR, and a template with text after the Xs is not filled in and makes a file with the literal name; use a template that ends in the Xs.
References
Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.