When measuring a bolt, it’s best to use calipers and a thread checker. You can probably work it out with a metric ruler though.
For example, take this M5-0.8 x 16mm:
The bolt size is M5. You determine this by measuring the diameter of the shaft in mm to come up with the "M5" value. The socket wrench the bolt uses does not determine the bolt size. This is a common mistake.
The bolt length is measured from the end of the threads to the bottom of the bolt head. That's the length of the shaft, excluding the head. In the example it is 16mm long.
The thread pitch is the distance between threads measured in millimeters along the length of the bolt. Determining the thread pitch can be difficult without a thread checker tool. The bolts on this sight use the standard thread pitch per size. I’m sure there are some odd balls out there but generally speaking, bicycles and bicycle parts use bolts with these thread pitches.
M5=0.8 thread pitch
M6=1.0 thread pitch
M7=1.0 thread pitch
M8=1.25 thread pitch
There is a lot variability in what size bolts manufacturers use use for their parts. Here is a general guide fo the most common bolt sizes.
One common mistake is thinking the size wrench you use correlates to the size of your bolt. For example the bolt used to attach your bottle cage is not an m4 because you use an m4 wrench. Bottle cages always use m5 bolts.