Step 1: Download the version of macOS or OS X that you want to use.
Step 2: Open Disk Utility and insert your USB drive.
Step 3: Click the View dropdown at the top right of the window, and choose Show All Devices.
Step 4: Find your USB drive in the list and select the highest parent. (This is usually the name of the device’s manufacturer.) Select it.
Step 5: Click Erase at the top and give it a name - this can be the name of the OS you’re installing, such as Yosemite. Choose Mac OS Journaled (Extended) and click Erase. This may take some time.
Step 6: Choose your new drive paritiion in the sidebar list - this will be the one with the name you just typed.
Step 7: Choose Restore at the top. Then, open Finder and find the DMG you downloaded. Drag it into the dropdown in Disk Utility. Click Restore, and wait. This will take a long time, depending on the size of the OS and the speed of your source file’s location and the speed of your USB drive.
Step 8: Once you’re done, unplug the USB and shutdown your Mac. If you’re installing to different Mac to the one you made the USB on, you need to shut down the destination Mac.
Step 9: Insert the USB to your destination Mac. Power it on whilst holding Option ⌥. Then, select the USB drive using the arrow keys and press enter to boot. The USB will typically say ‘Install OS X’ or ‘Install ESD’. It will also have a different icon to the internal drive, and is probably the only device other than your internal storage.
Step 10: Use the macOS / OS X installer to install the OS. You may have to use Disk Utility to erase the internal drive first.