Surveys are better instruments for revealing demographic and attitudinal bases of support for different options than they are at predicting the outcome of tight races.
It is important to stress that the polls were not far off. In fact, in both cases the actual results were within the margin of error of the most reliable polls conducted closest to election day. So strictly speaking, the polls were not wrong.
That said, most polls leave much to be desired, methodologically speaking, in two ways: (a) they tend to be conducted via telephone as opposed to in person, which is cheaper but also much less reliable; and (b) they tend to have relatively small sample sizes, again the cheaper but less reliable option.
The predicted outcomes are based on difficult, often untransparent, inferences about who will actually go to vote on election day. These inferences tend to be based on the assumption that previous electoral behaviour patterns of different demographic groups will be repeated.
In cases like Brexit or Trump where establishment opinion lines up clearly on one side, there is also a not insignificant possibility that some people tell pollsters what they think is the “politically correct” response.