As you've seen in the 'Operating System' section, the console never runs a game straight away; it first performs a series of checks that prevent the execution of unauthorised cartridges and also make sure the cartridge is correctly inserted.
To be able to pass these checks, games had to include a copy of Nintendo's logo (in the form of tiles) within the ROM header . This way, Nintendo could make use of copyright and trademark laws to control distribution. Pretty clever, huh?
Conversely, Sega v. Accolade later awarded companies the right to use copyrighted logos for this kind of requirement, as it was deemed fair use.
That being said, additional anti-piracy measures could be implemented inside games. Examples included checking the SRAM size (as it's typically larger in bootlegs) and checksumming the ROM at random points during gameplay (to spot modifications in the game's code).