Long story (skip to TL;DR: if painful)
We have been wrestling with this problem as well on specific type of input data.
Our customers create user accounts to their users in our web application, and some of them "cut and paste" the username (and email address and phone number) from emails or Word documents to the web form.
At one point a customer contacted our helpdesk and said that the user can't log in, but the customer can.
The reason why the customer could log in was that she cut and pasted the "username " also to the login form, and the user typed the given "username". So the user account was unintentionally created with a trailing whitespace in the end of the username.
The first time that happened we just trimmed the whitespace from the database, but after few more of these we set trimming for the user account creation on the server side. Because no-one intentionally wants a trailing whitespaces in the username (or email or phone number), and our customers seem to do a lot of copy-pasting with that type of information.
Copy-pasting data and smart phone keyboards very often unintentionally add trailing spaces after words in input fields.
Depending on the type of input:
if it's 100% sure no-one intentionally wants trailing whitespaces on the input data trim them automatically (usernames, email addresses, phone numbers).
if it's not that clear (free form text inputs) then give some type of error (and maybe provide an action there to trim the input(s) for the user)