Infinite or near-infinite Master Balls are one of the most common cheat requests in Pokémon, and the standard method is an Action Replay device used with the Nintendo DS cartridges.
You enter the code into the Action Replay software, load it onto the device, then start the game with the code enabled. Item codes generally overwrite the quantity of an item in a specific pocket of the bag, which is why they only work on the correct game version — a code for Diamond will not behave properly on Platinum.
Two cautions worth taking seriously: cheats can flag a save as modified, which matters if you ever want to trade or battle online, and it is always wise to back up a save before using codes at all.