Most MIDI devices have a third port "MIDI thru", and the input is passed directly to this port so you can chain devices. Sometimes the output port doubles as thru. Each device listens to messages on its own channel. For more complicated setups there are midi patchers/mixers available.
There's one downside to this: if you chain too many devices you might run into latency problems.