Its free because the files are already public via GitHub, so it doesn’t cost GithHub much extra money to run.
Anyone can go to a public GitHub repo and see the files, right? So if the GitHub Pages website’s files are in a public repo, all GitHub has to do is slap a domain name in front of those files.