Tamping is integral to making fine espresso. The purpose of tamping your grounds in the portafilter is to create a uniform surface through which brew water can flow evenly. When your grounds are in the portafilter basket, lightly shake the portafilter from side-to-side to evenly distribute the grounds, and level off by brushing your finger across the surface. When tamping, you want to apply a strong but measured amount of pressure to the grounds on a level surface for an even tamp. A good starting point would be about 30 lbs of pressure.
Depending on your shot times, you will want to adjust your grind size to make adjustments to get ideal extraction times. Keep your tamp pressure consistent. If you are using preground then you will want to adjust the amount of coffee you put in the filter basket and the tamp pressure. Be sure not to overtamp or exert too much pressure on the puck of coffee or you can cause channeling. Channeling causes a channel or fracture to open in the puck through which all of your brew water will try to flow, making for a very uneven or poor extraction. Ideally, you also want your tamped grounds to be level so that water flows evenly throughout the puck. If tamped unevenly, water will flow to one side of the puck.
The correct tamper diameter is typically determined by the machine you’re using. Semi-automatics tend to have a varying range of sizes depending on the basket size of their respective portafilters, but Prosumers and higher end single boiler home machines will opt for the commercial standard of 58mm. You always want to match your tamper diameter with the diameter of your portafilter. Too small and you won’t tamp all of your grounds leading to channeling on the outside of the coffee puck. Too large and the tamper won’t fit in the basket at all.