Where do the values in a template come from, and where do template files usually live?
From any variable available to the task (play vars, inventory, vars_files, registered results) and from the host's facts. Templates are usually kept in the project's templates/ directory with a .j2 extension, which is a convention, not a requirement.
A template consists of three parts: plain data (text copied as is), variables ({{ ssh_port }}) and expressions ({{ users | length }}, {% if ... %}). Rendering replaces variables and expressions by their values for the host being configured.
Because the template is rendered per host, host-specific sources work naturally: host_vars, group_vars and especially facts such as ansible_facts['fqdn'] all resolve to that host's values.
Conventions that help:
templates/next to the playbook (or inside a role): thetemplatemodule looks there, sosrc: sshd_config.j2is enough..j2at the end of the name (sshd_config.j2) tells every reader that the file is not deployed as is. Jinja2 doesn't care about the extension.
Go deeper:
Templating (Jinja2) (official docs) โ variables and facts in templates.