Polling is an inexact science. However, if given a large enough sampling population, good neutral questions, and assuming everyone tells the truth, then the science should be good and the data pretty darn accurate. However, much of this didn't happen during the polling for the Trump election.
There were several factors involved, but let's talk about the three biggest.
The media didn't go long enough or deep enough country-wide when polling to truly determine whether Trump or Hillary Clinton would win. By far, the going in expectation was for Hillary to win and so quite quickly most polling gradually shifted to trying to determine merely by how much she was going to win. This is called confirmation bias, and it leads to horrible results predictions. Further, some on-the-fence voters saw this early and very visible confirmation bias and thought to themselves that it was arrogant and premature, and so voted the other way just to rebel a bit. Remember too, there is that streak in the American character that always wants to root for the underdog, especially an oddball underdog. In the end, this confirmation bias became the most inaccurate and embarrassing disaster in polling science since the 1948 elections when the Chicago Tribune headlined the famous "Dewey Defeats Truman" result, which of course never happened.
The second factor is that a large bloc of voters that Democrats thought of as normally in their pocket defected and went to the Republican side. Trump ran a fairly effective campaign in industrial rust-belt states, where his message resounded to unemployed and under-employed workers that used to have high-paying jobs in auto plants, steel mills and factories. They felt the only jobs people on the coasts were paying attention to were high-tech and service industry jobs, and they felt left behind. Trump's plain non-politically correct way of speaking, combined with a slogan of "Making America Great Again" resounded deeply with this bloc of voters, and many switched sides.
Lastly, polling assumes people are telling the truth, and increasingly this is not the case. People are polled and surveyed to death these days on everything, and survey fatigue leads to sloppy or rushed answers just to make the survey go away. More importantly however, we live in an age of a call-out culture. If someone doesn't like your opinion and finds out about it, they may broadcast it to the world through social media and you are then the recipient of much backlash and abuse from partisans on the other side. In some cases people lose their jobs. To avoid this grief, many poll respondents check to see which way the wind is blowing and just say they are doing that, then vote their true conscience in the privacy of their voting booth on election day. This phenomenon is especially true in the heartland of the U.S. where for 200 years there has always been an ethos of don't tell people what you got, don't discuss politics, mind your own business, and keep your opinions to yourself. When exposed to opposing in-your-face shouting confrontation politics these group of people tend to hunker down, go silent, and then quietly wait for their moment in the voting booth. Polls will never be able to capture this phenomenon.
There were many other factors of course, like performance in the debates and the news story of the week that seemed to pop up for each candidate, but these three reasons had the deepest roots and the longest legs.