To attack a monster, try moving into the space it occupies. This will initiate a melee attack using the weapon you are wielding, or your hands if you aren't holding a weapon. But be warned: some monsters automatically retaliate to melee attacks with a passive attack that can hurt you greatly. Doing this with your pet is usually safe - you will attempt to displace it rather than attack it (unless your weapon is bloodthirsty). If you want to attack your pet for some reason, you can use the Shift-F command to force your next input to be interpreted as a melee attack.
You can use a ranged attack to attack from a distance. Use f to fire a weapon or item that you have quivered, or use t to manually choose something to throw. You can throw anything, but some projectiles, like darts or shuriken, are designed to be thrown. Other projectiles are meant to be used with a launcher. For example, wield a bow to fire arrows.
Magic is another option for attacking a monster. If you have an attack spell memorized, and sufficient power, you may cast it using the Shift-Z command. If you have an attack wand, you can zap it with z. Frost horns or fire horns are also sources of magical attacks. Use a to apply them.
To know how likely you are to hit in combat (and conversely how likely monsters are to hit you), see the Armor Class page. The Weapons page will tell you all about the different kinds of weapons in terms of damage and other properties, but note that for ranges of weapons you may need to look elsewhere (for instance, the range of your bow is actually defined by the type of Arrow, it would seem, and most objects of reasonable throwing weight seem to have similar ranges as arrows).
It is important to note that you don't have to attack every monster you encounter. Some are best left alone or actively avoided. As the proverb states, discretion is the better part of valor.