Dear TeX, please end the paragraph, if you're typesetting one, and make a rule with the default height (0.4pt) and depth (0pt), as wide as the box where the rule will end up in.
Now, if I compile the following document
\documentclass{article}
\showoutput
\begin{document}
\hrule
\end{document}
I get the following in the log file:
Completed box being shipped out [1]
\vbox(633.0+0.0)x407.0
.\glue 16.0
.\vbox(617.0+0.0)x345.0, shifted 62.0
..\vbox(12.0+0.0)x345.0, glue set 12.0fil
...\glue 0.0 plus 1.0fil
...\hbox(0.0+0.0)x345.0
..\glue 25.0
..\glue(\lineskip) 0.0
..\vbox(550.0+0.0)x0.0, glue set 539.94232fil
...\write-{}
...\glue(\topskip) 9.6
...\rule(0.4+0.0)x*
...\glue 0.0 plus 1.0fil
...\glue 0.0
...\glue 0.0 plus 0.0001fil
..\glue(\baselineskip) 23.55556
..\hbox(6.44444+0.0)x345.0, glue set 170.0fil
...\glue 0.0 plus 1.0fil
...\OT1/cmr/m/n/10 1
...\glue 0.0 plus 1.0fil
The first line after the banner describes the page being shipped out, which has height 633pt (\textheight plus the header and the footer), no depth and width 407pt (\textwidth). Then
..\vbox(12.0+0.0)x345.0, glue set 12.0fil
...\glue 0.0 plus 1.0fil
...\hbox(0.0+0.0)x345.0
is the (empty) header, followed by 25pt of space (\headsep) and \lineskip (because the following box is very high).
The following box is the page body
..\vbox(550.0+0.0)x0.0, glue set 539.94232fil
that has height 550pt (the \textheight) and width 0pt, because it contains no box: just a default \write that's always added to the first page, the \topskip, the rule and fill up glue. What follows, that is,
..\glue(\baselineskip) 23.55556
..\hbox(6.44444+0.0)x345.0, glue set 170.0fil
...\glue 0.0 plus 1.0fil
...\OT1/cmr/m/n/10 1
...\glue 0.0 plus 1.0fil
is the footer with the page number 1 centered.
The rule is represented by
...\rule(0.4+0.0)x*
meaning exactly what I described at the beginning: the width is determined by the (vertical) box the rule ends up in.
Note that \hrule is a vertical command, so it ends the paragraph being typeset; TeX adds no interline glue before and after the rule.
You can try
\hbox{aaa}
\hrule
\hbox{bbb}
instead, getting

because the box width is determined by the widest box inside. Since no paragraph is started, the rule ends up with the same width as the bottom \hbox.
TeX primitives are very peculiar, aren't they?
\begin{document} \leavevmode\hrule \end{document}– Steven B. Segletes Aug 18 '15 at 11:33\hrulefillif you want it to work even in vertical mode. – Steven B. Segletes Aug 18 '15 at 11:56\hruleforces vertical mode. – egreg Aug 18 '15 at 15:48\rule{\linewidth}{.4pt}also work in vertical mode. – Fran Aug 18 '15 at 19:01