Fly rod-line size is determined by the manufacturer. They decide how the rod is to preform at a set distance then they try to cast several size lines to get the maximum performance from the rod. Each manufacturer has a slight difference standard but I can give a example.
Rod company XYZ throws 45 feet of 4,5 and a 6 weight line on there 9 foot medium action rod. It works best (loads and cast) best with the 6 weight line. They stamp the rod for a 6 weight rod.
If you throw less then 45 feet or even use larger flys with weight you could use a larger line size to get the same action in casting from the rod. However if you cast more then 45 feet of line you may want a line one size smaller ( 5 weight) to get the rod to load properly.
Last is is casting in heavy wind you can use a line one size smaller do to less wind resistance.