A cartridge in it’s most basic form is just a convenient way to plug a ROM chip into the CPU of a console.
ROM is very simple. You give ROM a number called an “address”, and it returns a number that is the “data” at that address.
Well, since we’re working with digital electronics, these numbers are specified as binary numbers. For old ROM chips found in carts, the bits for both the address and data numbers have a direct mapping to the pins on the ROM.
For example, let’s say we have a ROM that can take a 15-bit address and returns 8-bit data, for a total of 2^15-1 bytes of memory. In that case, there are 15 address pins on the ROM chip labeled A0-A14 and 8 data pins labeled D0-D7. The pins are directly connected to the contacts on the cartridge.
Everything gets reduced down to a number on a computer, both data and code alike. Loosely speaking, a computer is designed to execute instructions by keeping track of the address of the instruction to execute (called the “program counter” or PC), fetching the number at that address, interpreting the number as an instruction, executing that instruction, incrementing the PC, and doing it all over again.
In this way a game can be stored on a ROM, which a console can read to execute code and get data.
there might be a chip-select pin or output-enable pin on a ROM chip too, but those are minor details.