Multiple definition of a variable in a multi-file sketch

sthudium:
However, I don't understand why the #ifndef-#endif construct in the header file did not prevent multiple variable definitions when that header file is included in multiple files.

So you have to understand how the preprocessor builds Translation Units.

A translation unit is the basic unit of compilation in C++. It consists of the contents of a single source file, plus the contents of any header files directly or indirectly included by it, minus those lines that were ignored using conditional preprocessing statements.

By defining and declaring the variable in your header, every translation unit will then have its own copy of that variable... you #include the header twice (once in your .ino and once in your .cpp file) so when the compilation comes together, you then have two like-named variables that cannot be resolved during the compilation (literally "Putting Together") of the Translation units.

The #include guards simply prevent the entire header from being copied into a Translation Unit more than once.