Package listings uses in its default setting a fixed width for each letter. The width is quite large, because letter such as M must fit in. For source code listings the requirements for the vertical alignment are not very high. Thus a flexible columns layout often looks much better. See 2.10 Fixed and flexible columns of the documentation.
Flexible settings use a column width of 0.4em instead of 0.6em for setting flexible.
Source code also contains lots of words. Setting the words in a font with variable glyph widths improve the look of the words. Even for typewriter fonts there are variable width fonts. For example, the Latin Modern font family (successor of Computer Modern) contains such a variable typewriter font, see the examples below.
The line length usually shrinks, because letters with smaller widths (e.g. i) need less space as in a font with equal width for all glyphs.
The following shows an example with non-variable/variable typewriter font and the three settings for the columns. It also shows the problematic alignment cases for the flexible column layouts.
\NeedsTeXFormat{LaTeX2e}
\documentclass[12pt,a4paper]{article}
\setlength{\overfullrule}{5pt}
% use variable width typewriter font
\usepackage[T1]{fontenc}
\usepackage[
variablett
]{lmodern}
\usepackage{listings}
\lstset{basicstyle=\ttfamily}
\begin{document}
\newcommand*{\test}[1]{%
\lstinputlisting[
language = {[LaTeX]TeX},
literate = {TeX}{\TeX}3
{LaTeX}{\LaTeX}4
{LaTeX2e}{\LaTeXe}6,
moretexcs = {NeedsTeXFormat,lstset,lstinputlisting},
columns = {#1},
]{\jobname.tex}%
}
\renewcommand*{\ttdefault}{lmtt}% not variable
\test{fixed}\hrule\test{flexible}\hrule\test{fullflexible}
\newpage
\renewcommand*{\ttdefault}{lmvtt}% variable
\test{fixed}\hrule\test{flexible}\hrule\test{fullflexible}
\end{document}
The following images contains the source code listings in the following settings
fixed/non-variable fixed/variable
flexible/non-variable flexible/variable
fullflexible/non-variable fullflexible/variable

Another possibilty is the use of a smaller font size for the listings to reduce the line width.