Grams are a unit of weight like pounds or ounces. Because of their very small size (it takes 28 grams to equal a single ounce), grams can be used to accurately measure carbohydrate.
Simply weighing foods does not tell how much carbohydrate it contains because most foods are not purely carbohydrate. For example, even though 224 grams(one cup) of milk, a 160 gram slice of watermelon, a 14 gram rectangular graham cracker (two squares), and 12 grams (one tablespoon) of sugar have different weights, they all contain exactly 12 grams of carbohydrate. The milk and watermelon contain water, while graham crackers have other ingredients. Only the sugar is all carbohydrate.
Despite their different weights, they all contain 12 grams of carbohydrate and will require the same carb bolus to cover them. Knowing a food's weight and the percentage of its weight that is carb allows precise carb measurement. Knowing your carb factor, which is how many carbs are covered by one unit of insulin, allows precise insulin dosing.