There are two envelopes for each category.
Two people, Martha Ruiz and Brian Cullinan, count the votes and know the winners in advance. Each has a full set of envelopes containing cards with the winners' names - just in case something happens to the other set.
Those people - from accountancy firm PwC - stand on either side of the Oscars stage and hand the envelopes to the award presenters just before they step on stage.
This article shares some thoughts on this.
The Academy takes the process of tabulating winners and then keeping their identities a secret very seriously. They entrust it to the accounting firm PricewaterhouseCoopers. Since 2005, the same two men have been in charge of handling the final tallies and guarding the winners’ names: Rick Rosas and Brad Oltmanns(Correction: now Martha Ruiz and Brian Cullinan) Also known as the “men with the briefcases on the red carpet,” their responsibilities are quite real and not mere ceremony.
The designated two people in charge then assemble the final count. Winners are written on cards in a secret location and then placed in the sealed Oscars envelopes, which are then put under tight lock and key until the day of the ceremony. As awards presenters enter the stage, they receive the sealed envelopes and legitimately discover the winner’s identity for the first time as they tear open the seal. As such, their surprises are genuine, as are the actual winners they announce.
(correction and emphasis mine)
PricewaterhouseCoopers, the company responsible for the Academy voting, said in a statement:
"We sincerely apologise to Moonlight, La La Land, Warren Beatty, Faye Dunaway and Oscar viewers for the error that was made during the award announcement for Best Picture.
"The presenters had mistakenly been given the wrong category envelope and when discovered, was immediately corrected. We are currently investigating how this could have happened, and deeply regret that this occurred.