Most of what you need is explained at https://en.bitcoin.it/wiki/Merged_mining_specification
When you construct the block for the primary chain (e.g. bitcoin) you need to include a merged mining header in the generation transaction (coinbase). The format of this data is explained at the URL above under "Merged mining coinbase".
If, while hashing the primary chain, you get a hash that is below the target for the auxiliary chain (e.g. namecoin) you can create a block on that chain using getmemorypool (assuming the daemon for that chain supports getmemorypool).
To submit a merged mining block, for namecoin you need to do two things that you wouldn't do when mining non-merged.
The first is to set the auxpow bit in the version field of the block header.
BLOCK_VERSION_AUXPOW = (1 << 8)
The second difference is that you insert an auxpow between the header and the transactions. The binary layout of the auxpow is explained at the URL above under "Aux proof-of-work".
The auxpow bit in the version field tells namecoin that after the block header there will be an auxpow before the transactions.
In theory other alt coins may need to have their merged blocks formatted differently and/or submitted differently to their daemon.