The answer depends on the compiler and toolchain. If the question is about Visual C++ then you should tag it as such.
For VC++ the target minimum OS version supported is controlled by a set of macros described at Using the Windows Headers.
In the simplest case, to target XP you would need to add
#define _WIN32_WINNT 0x0501
before including any of the windows headers, or add /D_WIN32_WINNT=0x0501
to the compiler command line.
Replace 0x0501
with 0x0502
if you don't need to support the original release of XP (without any service packs applied), per this note in the same linked article.
Note that some features introduced in the latest version of Windows may be added to a service pack for a previous version of Windows. Therefore, to target a service pack, you may need to define _WIN32_WINNT with the value for the next major operating system release. For example, the GetDllDirectory function was introduced in Windows Server 2003 and is conditionally defined if _WIN32_WINNT is 0x0502 or greater. This function was also added to Windows XP with SP1. Therefore, if you were to define _WIN32_WINNT as 0x0501 to target Windows XP, you would miss features that are defined in Windows XP with SP1.
P.S. It's you who controls the sources and compilation flags of your own code, so I am not sure how to answer this part of the original question.
What conditional compilation flags can I check to determine which windows OS the code is being compiled for?