Is there any way to create a binary that will run on many executable file formats (for example, Mach-O and ELF)? Even if it's just a file that contains both and runs the right one, I'd like to make an executable that works on both Linux and Mac.
Short answer: no, because each binary format starts with a specific sequence ("magic number") that identifies the format. ELF binaries start with 0x7F 45 4C 46, Mach-O single-arch binaries with 0xFE ED FA CE or 0xCE FA ED FE (depending on byte order), and Mach-O fat binaries with 0xCA FE BA BE. Since the binary can't start with more than one of these, it can't be valid in multiple formats.
Now, it might be possible to do something trickier, like having a wrapper shell script that somehow convinces the loaders to execute part of itself... But that's getting beyond my expertise (and will depend not only on the binary formats you're trying to load, but the OSes you're trying to load them on as well...)