The Metropolitan Transportation Authority reached a contract agreement on Thursday with unions representing workers on the Long Island Rail Road, averting a strike that would lead to nightmare commutes for many of the railroad’s 300,000 daily riders.