The fact that the second code snippet is raising an error is a good thing.
It prevents you from corrupting data by mistake.
In the first code snippet, however, SQL Server will silently trim the string due to implicit conversion rules.
Whenever you attempt to populate a variable with a data that has a different data type, SQL Server will attempt to implicitly convert the data type to the data type of the variable.
This is well documented in the Converting Character Data section of the char and varchar (Transact-SQL) page:
When character expressions are converted to a character data type of a different size, values that are too long for the new data type are truncated.
This does not happen when inserting into a table, providing ANSI_WARNINGS is set to ON (which is the default state).
When ANSI_WARNINGS is set to ON, you get the
String or binary data would be truncated
error message.
When it's set to OFF, however, the implicit conversion will silently truncate the data:
set ansi_warnings off;
declare @temp1 varchar(5)
declare @tm1 table (a varchar(5))
insert into @tm1
values('abcdefghijkl')
select * from @tm1
Result:
a
abcde
Note: The ansi_warnings state does not have effect the implicit conversion when setting a variable value - it will always be truncated regardless of the ansi_warnings state.