What does if __name__ == "__main__" do, and what is __name__ set to in each case?
The guard is a comparison against the literal string __main__, and __name__ is a module attribute whose value depends on how the module was loaded.
When you run a file directly, Python executes it as the main module and sets __name__ to the string __main__. When the same file is imported by another module, __name__ is set to the module's own name, such as greet, so the comparison is False and the guarded block is skipped.
That difference is the entire purpose of the guard. It lets one file hold both a set of definitions that other modules can import and a set of statements that should only run when the file is executed as a program. Without it, importing the module would also run the top-level code, and that is why scripts used to print things or start servers the moment they were imported.
The usual shape puts the executable work inside a function called main and then calls it from inside the guard. That way the module stays importable and free of side effects, and there is one obvious place to look for the entry point.
One trap is worth mentioning in an interview. If a module in your project is imported under two different names, for example once as package.module and once as module, it will be executed twice, because sys.modules uses the name as its key. The main guard does not prevent that, because in both cases the module is being imported rather than run. Keeping your import paths consistent is what actually solves it.