As always, you want the fantastic CBA FAQ, in this case, Question 53 "Is there a limit to the length of a contract?":
The maximum years and raises depend on the type of contract, however the "standard" maximum length is four seasons [...] The following are exceptions to the standard:
[ ...a number of exceptions, but the longest is: ]
Extensions (Designated Veterans) 6 [years]
"Why?" is a more complicated question. The maximum contract length was introduced in order to prevent teams crippling their teams for multiple years by being tied into a high-priced contract with a veteran who is years past their prime - see e.g. Rick DiPietro for what happens if this kind of restriction doesn't exist.
While the contract length restriction has accomplished that aim, it's also had the effect of moving a lot of power to the players as teams can't lock up their legitimate superstars for long contracts - see in particular LeBron James's multiple moves, and Kevin Durant's move to the Warriors (and his potential leaving).