Hi Jessica,
The 40' of fence has to be enough to enclose one side parallel to the barn and the same length as the barn plus two other identical sides.
Let's call B the length of the barn and W is the length of the other two sides
B + 2W = 40
BW = area of the garden
The largest area of a rectangle is when that rectangle is a square.
To prove this, imagine a garden that is 100 square feet. This implies dimensions of 10'x10', which is a perimeter of 40'. Let's say we increased the length by 1 foot and decreased the width by 1 foot, you would still have the same perimeter (2)(11+9) = 40, but the area would only be 99 (11x9).
This means that all sides are equal. So,
B+2W = B+ 2B = 3B=40
B = 40/3 = 13.333'
W = 13.333'