The needspace package provides an easy means to accomplish this. It defines the command \needspace{<length>} that checks to see whether <length> is still available on the remainder of the page. If not, then a \break is issued, otherwise nothing is done. I've taken the exact code from the command \needspace, and adapted it to actually condition on whether or not there is enough space:
\usepackage{xparse}% http://ctan.org/pkg/xparse
...
\NewDocumentCommand{\ifneedspace}{m O{\relax}}{%
\par \penalty-100\begingroup
\setlength{\dimen@}{#1}%
\dimen@ii\pagegoal \advance\dimen@ii-\pagetotal
\ifdim \dimen@>\dimen@ii
\break
\else
#2
\fi\endgroup%
}
This new \ifneedspace{<length>}[<success>] command also checks to see whether <length> is available on the page. If this is the case, then <success> is executed. This condition is optional (hence the square brackets [ ]) and defaults to \relax (which does nothing). If there actually is not enough space on the page, it only executes \break. \ifneedspace is defined using the interface provided by xparse.
To make things a little more fancy, I've added the \myrule definition from How do I insert a border below text? as well. This requires the xcolor package for a selection of colours.
You can play around with the required <length>, but it seems like 6\baselineskip works. This allows for enough space of the rule (at 2pt thickness and 6pt total gap plus the section heading). Different lengths will, of course, require a different <length> to be used.
Here is the entire minimal working example illustrating the above concepts:
\documentclass{article}
\usepackage{needspace}% http://ctan.org/pkg/needspace
\usepackage{xparse}% http://ctan.org/pkg/xparse
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\newcommand{\Text}{% Dummy text
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas
vitae libero lectus. Sed justo nunc, bibendum at euismod vel, molestie
vestibulum ipsum. Aenean tincidunt vestibulum nulla, ut feugiat mi
malesuada quis. Vestibulum eu tincidunt dui. Aliquam erat volutpat.
Nam mauris nibh, placerat in blandit sed, scelerisque quis justo.
Duis ac libero nec leo ullamcorper pellentesque sed at libero.\par}
\makeatletter
\NewDocumentCommand{\ifneedspace}{m O{\relax}}{%
\par \penalty-100\begingroup
\setlength{\dimen@}{#1}%
\dimen@ii\pagegoal \advance\dimen@ii-\pagetotal
\ifdim \dimen@>\dimen@ii
\break
\else
#2
\fi\endgroup%
}
\NewDocumentCommand{\myrule}{O{1pt} O{3pt} O{black}}{%
\par\nobreak % don't break a page here
\kern\the\prevdepth % don't take into account the depth of the preceding line
\kern#2 % space before the rule
{\color{#3}\hrule height #1 width\hsize} % the rule
\kern#2 % space after the rule
\nointerlineskip % no additional space after the rule
}
\makeatother
\begin{document}
\section{First section}
\Text \Text \Text \Text \Text \Text
text \par text % \par text
\ifneedspace{6\baselineskip}[\myrule[2pt][3pt][orange]]
\section{Second section}
\Text \Text \Text \Text \Text \Text \Text
\end{document}

If you uncomment the additional \par text, you obtain the following output:

It would be possible to incorporate the \ifneedspace command within the standard \section command using letltxmacro. Or, define your own \Section command that incorporates the above.
\needspaceif I redefine\@secpenaltyto be 10000 to really prevent a break between the rule and the heading. The real insight here, for me, is the use of\leadersto produce the rule: that makes TeX treat it as glue, so it's automatically discarded when it follows a page break. – Wyzard Oct 21 '11 at 07:21