How do code coverage tools like NCover know what parts of the code were executed and what parts were not?
From this source:
NCover uses the .NET Framework profiler API to monitor an application's execution. When a method is loaded by the CLR, NCover retrieves the IL and replaces it with instrumented IL code
So in short, it hooks itself into the just-in-time compilation.
Not all tools work the same way though. Other tools work by modifying the bytecode of your application after the code has been compiled.