What does the .def file do?

tex packagename.ins

you always get a bunch of files, including the .def file. It's also mentioned in the .sty file. Apparently it's a definition being used for something.

What exactly is it for though? And can it be omitted when sharing the LaTeX project?

-
It depends on the package concerned. Usually, any .def will be needed, and so you should install them. –  Joseph Wright May 13 '12 at 14:01
–  Martin Scharrer May 13 '12 at 14:09

Usually files with the extension .def collect long lists of similar definitions, that would make a .sty file long and unreadable, or that can possibly be shared with other packages.

If tex packagename.ins creates a .def file, then this is part of the package structure and mustn't be omitted.

A typical example are the <encoding>enc.def files, such as t1enc.def loaded by fontenc:

\ProvidesFile{t1enc.def}
[2005/09/27 v1.99g
Standard LaTeX file]
\DeclareFontEncoding{T1}{}{}
\DeclareTextAccent{\`}{T1}{0}
\DeclareTextAccent{\'}{T1}{1}
\DeclareTextAccent{\^}{T1}{2}
\DeclareTextAccent{\~}{T1}{3}
<...>
\DeclareTextSymbol{\NG}{T1}{141}
\DeclareTextSymbol{\OE}{T1}{215}
\DeclareTextSymbol{\O}{T1}{216}
\DeclareTextSymbol{\SS}{T1}{223}
\DeclareTextSymbol{\TH}{T1}{222}
\DeclareTextSymbol{\ae}{T1}{230}
\DeclareTextSymbol{\dh}{T1}{240}
\DeclareTextSymbol{\dj}{T1}{158}
\DeclareTextSymbol{\guillemotleft}{T1}{19}
\DeclareTextSymbol{\guillemotright}{T1}{20}
<...>

where having the long list in a separate file makes it more easily maintainable and searchable. Similarly inputenc uses .def files for lists of pairings between input characters and their connected output.

Some other packages use some .def files for "context dependent" code. Examples are graphics/graphicx and hyperref that put the engine dependent code (for latex/dvilualatex, pdflatex/lualatex and xelatex) in .def files such as pdftex.def and dvips.def (graphics) or hpdftex.def and hdvips.def (hyperref).

Other packages, notably bidi, xepersian or minitoc, use .def files to load code depending on what packages are loaded in the document.

In some cases perhaps an extension such as .sto ("style option", modelled after .clo used by the standard classes) might have been preferable, but tradition has its influence. So long as the names are unique, the extension used is irrelevant.

-
Also compiler dependent things are put into .def files and the correct one is then loaded (e.g. pdftex.def if pdflatex is used) –  Martin Scharrer May 13 '12 at 14:09