The following code puts both the section number and the labels of theorem environments in the margin by putting them in right-aligned boxes of zero width (and zero depth).
\documentclass{article}
\newlength{\marginlabelsep}\setlength{\marginlabelsep}{1em} %% <- change to a nice value
\usepackage{titlesec} %% <- To put the section # in the margin
\titleformat{\section}{\normalfont\Large\bfseries}
{\llap{\thesection\hskip\marginlabelsep}}{0pt}{}
\usepackage{amsthm}
\newtheoremstyle{mythmstyle} %% Name
{} %% <- Space above (empty = default = \topsep = 8.0pt plus 2.0pt minus 4.0pt)
{} %% <- Space below (empty = default = \topsep = 8.0pt plus 2.0pt minus 4.0pt)
{\itshape} %% <- Body font
{} %% <- Indent amount (empty = no indent, \parindent = just that)
{\bfseries} %% <- Thm head font
{} %% <- Punctuation after thm head
{0pt} %% <- Space after thm head (or " " or \newline) (default: 5pt plus 1pt minus 1pt)
{\vtop to 0pt{\llap{\thmname{#1}\hskip\marginlabelsep}
\llap{\thmnumber{#2}\hskip\marginlabelsep}}\thmnote{#3\\}%
} %% <- Thm head spec (empty = default ~= \thmname{#1}\thmnumber{ #2}\thmnote{ (#3)})
\theoremstyle{mythmstyle}
\newtheorem{theorem}{Theorem}[section] %% <- Numbering subordinate to sections
\newtheorem{lemma}[theorem]{Lemma} %% <- Uses same counter as theorem
\usepackage{lipsum} %% <- for \lipsum
\begin{document}
\section{My section}
\lipsum[66]
\begin{lemma}
\lipsum[66]\lipsum[75]
\end{lemma}
\begin{theorem}[A beautiful theorem]
\lipsum[66]
\end{theorem}
\lipsum[66]
\end{document}

Explanation:
I am using the amsthm
package to create a custom theorem style.
The \newtheoremstyle
command is a little monstrous because it takes so many arguments arguments, so I keep these comments around to remind me what each of them does.
The most important argument for us is the last one, which describes the formatting of the theorem head. If this is left empty, it is effectively defined as
\thmname{#1}\thmnumber{ #2}\thmnote{ (#3)})
The arguments #1
, #2
and #3
will be the theorem name, its number and the title/note, and \thmname
, \thmnumber
and \thmnote
either print their contents, or nothing if there is no name, number or title/note.
To move the theorem head into the margin we should replace this by some code that takes up no space and prints the theorem name+number in the margin on two separate lines, followed by the theorem note and a newline (if there is a note).
The TeX primitives \vbox
, \vcenter
and \vtop
can be used to print multiple boxes on top of each other, like this:
\noindent
Word \vbox{\hbox{A}\hbox{B}} word \vtop{\hbox{C}\hbox{D}} word.\\
Next line \vtop to 0pt{\hbox{E}\hbox{F}},\\ %% Note: <-, ^ avoid using \\
last line.

The argument of \vtop
has to be a sequence of boxes, so I've put the letters A, B, ... inside \hbox
es (simple horizontal boxes) here.
I've used \vtop to 0pt{...}
instead of just \top{...}
to create a box with zero depth because the next line would otherwise be pushed downwards (like the second line above). For more information on boxes, see e.g. this answer.
To put the theorem label in the margin, I've used \llap{<contents>}
. This LaTeX command prints a box that contains <contents>
, takes up no space and sticks out to the left (\rlap
does the same, but sticks out on the right). Because it produces a box, it can be used directly inside \vtop
. Here is an illustration:
ggg\llap{OOO}hhh\rlap{OOO}jjj

I've added a horizontal space after \thmname{#1}
and \thmnumber{#1}
to make sure that the label is separated from the main text by\marginlabelsep
.
Remark:
Before this (hopefully) last edit, the final argument of \newtheoremstyle
was
{\vtop to 0pt{\hbox to -\marginlabelsep{\hss\thmname{#1}}
\hbox to -\marginlabelsep{\hss\thmnumber{#2}}}\thmnote{#3\\}%
} %% <- Thm head spec (empty = default ~= \thmname{#1}\thmnumber{ #2}\thmnote{ (#3)})
which was completely fine.
I've changed this because \llap
is a little easier to understand (and to explain!) than the effect of putting boxes with negative widths inside a \vbox
and what \hss
does.
article
documentclass or similar standard classes), with a theorem in it (and nothing else), and add an image that shows how this example document looks now and how you want it to look? Doing so will make it easier to understand what you mean and to try out possible solutions. You can edit using theedit
button below the question.