The primary technology that a military aircraft uses to lock and track an enemy aircraft is its onboard radar. Aircraft radars typically have two modes: search and track. In search mode, the radar sweeps a radio beam across the sky in a zig-zag pattern. When the radio beam is reflected by a target aircraft, an indication is shown on the radar display. In search mode, no single aircraft is being tracked, but the pilot can usually tell generally what a particular radar return is doing because with each successive sweep, the radar return moves slightly.
This is an example of the fire control radar display for an F-16 Fighting Falcon when the radar is in a search mode:
How Fighter Jets Lock On (and How the Targets Know)
Each white brick is a radar return. Because the radar is only scanning, not tracking, no other information is available about the radar targets. (There is one exception: The Doppler shift of the radar return can be measured, to estimate how fast the aircraft traveling towards or away from you, much like the pitch of an oncoming train's whistle can tell you how fast it's coming at you. This is displayed as the small white trend line originating from each brick.)
Note that the cursors are over the bottom-most brick (closest to our aircraft). The pilot is ready to lock up this target. This will put the radar into a track mode. In track mode, the radar focuses its energy on a particular target. Because the radar is actually tracking a target, and not just displaying bricks when it gets a reflection back, it can tell the pilot a lot more about the target. This is what the F-16's fire control radar display looks like when a target is locked:
How Fighter Jets Lock On (and How the Targets Know)
Along the top we have a lot of information about what our radar target is doing:
Its aspect angle (angle between its nose position and our nose position) is 160° to the left,
its heading is 190°,
its airspeed is 450 knots,
and our closure rate is 828 knots.
With this information, the pilot gets a much better idea of what the aircraft is doing, but at the expense of information about other aircraft in the area.
Note that in the above picture, the bottom-most (closest) target is locked (circle around it), the two targets further away are tracked (yellow squares), and there are two radar returns even further away (white bricks). This is demonstrating an advanced feature of modern radars, situational awareness modes. A radar in SAM combines both tracking and scanning to allow a pilot to track one or a small number of "interesting" targets while not losing the big picture of what other targets are doing. In this mode, the radar beam sweeps the sky, while briefly and regularly pausing its scan to check up on a locked target.
Note that all of this comes with tradeoffs. In the end, a radar is only as powerful as it is, and you can put a lot of radar energy on one target, or spread it out weakly throughout the sky, or some compromise in between. In the above photo you can see two vertical bars spanning the height of the display — these are the azimuth scan limits. It's the aircraft's way of telling you, "OK, I can both track this target, and scan for other targets, but in return, I'm only going to scan a 40° wide cone in front of the aircraft, instead of the usual 60°. Radar, like life, is full of tradeoffs.
An important thing to note is that a radar lock is not always required to launch weapons at a target. For guns kills, if the aircraft has a radar lock on a target, it can accurately gauge range to the target, and provide the pilot with the appropriate corrections for lead and gravity drop, to get an accurate guns kill. Without the radar, the pilot simply has to rely on his or her own judgement.
As an example of that, let's take a look at the F-16's HUD (heads-up display) when in the process of employing guns at a radar-locked target:
How Fighter Jets Lock On (and How the Targets Know)
It becomes incredibly simple; that small circle labeled "bullets at target range" is called the "death dot" by F-16 pilots. Basically, it represents where the cannon rounds would land if you fired right now, and the rounds traveled the distance between you and the locked target. In other words, if you want a solid guns kill, simply fly the death dot onto the airplane. Super simple.
But what if there's no radar lock? Well now the HUD looks like this:
How Fighter Jets Lock On (and How the Targets Know)
No death dot — but you still have the funnel. The funnel represents the path the cannon rounds would travel out in front of you if you fired right now. The width of the funnel is equal to the apparent width of a predetermined wingspan at that particular range. So, if you didn't have a lock on your target, but you knew it had a wingspan of 35 feet, you could dial in 35 feet, then fly the funnel until the width exactly lined up with the width of the enemy aircraft's wings, then squeeze the trigger.
And what about missiles? Again, a radar lock is not required. For heat-seeking missiles, a radar lock is only used to train the seeker head onto the target. Without a radar lock, the seeker head scans the sky looking for "bright" (hot) objects, and when it finds one, it plays a distinctive whining tone to the pilot. The pilot does not need radar in this case, he just needs to maneuver his aircraft until he has "good tone," and then fire the missile. The radar only makes this process faster.
Now, radar-guided missiles come in two varieties: passive and active. Passive radar missiles do require a radar lock, because these missiles use the aircraft's reflected radar energy to track the target.
Active radar missiles however have their own onboard radar, which locks and tracks a target. But this radar is on a one-way trip, so it's considerably less expensive (and less powerful) than the aircraft's radar. So, these missiles normally get some guidance help from the launching aircraft until they fly close enough to the target where they can turn on their own radar and "go active." (This allows the launching aircraft to turn away and defend itself.) It is possible to fire an active radar missile with no radar lock (so-called "maddog"); in this case, the missile will fly until it's nearly out of fuel, and then it will turn on its radar and pursue the first target it sees. This is not a recommended strategy if there are friendly aircraft in close proximity to the enemy.
As to the last part of your question — yes, an aircraft can tell if a radar is painting it or locked onto it. Radar is just radio waves, and just as your FM radio converts radio waves into sound, so can an aircraft analyze incoming radio signals to figure out who's doing what. This is called an RWR, or radar warning receiver, and has both a video and audio component. This is a typical RWR display:
How Fighter Jets Lock On (and How the Targets Know)
Although an aircraft's radar can only scan out in front of the aircraft, an aircraftcan listen for incoming radar signals in any direction, so the scope is 360°. A digital signal processor looks for recognizable radio "chirps" that correspond to known radars, and displays their azimuth on the scope. A chirp is a distinctive waveform that a radio uses. See, if two radios use the same waveform simultaneously, they'll confuse each other, because each radio won't know which radar returns are from its own transmitter. To prevent this, different radios tend to use distinct waveforms. This can also be used by the target aircraft to identify the type of radar being used, and therefore possibly, the type of aircraft.
In this display, the RWR has detected an F-15 (15 with a hat on it indicating aircraft) at the 7-o'clock position. The strength of the radar is plotted as distance from the center — the closer to the center, the stronger the detected radar signal, and therefore possibly the closer the transmitting aircraft.
Detected at the 12- to 1-o'clock position are two surface-to-air missile (SAM) sites, an SA-5 "Gammon" and an SA-6 "Gainful". These are Russian SAM launching radars and represent a serious threat. The RWR computer has determined the SA-6 to be the highest priority threat in the area, and thus has enclosed it with a diamond.
RWR also has an audio component. Each time a new radar signal is detected, it is converted into an audio wave and played for the pilot. Because different radars "sound" different, pilots learn to recognize different airborne or surface threats by their distinctive tones. The sound is also an important cue to tell the pilot what the radar is doing: If the sound plays once, or intermittently, it means the radar is only painting our aircraft (in search mode). If a sound plays continuously, the radar has locked onto our aircraft and is in track mode, and thus the pilot's immediate attention is demanded. In some cases, the RWR can tell if the radar is in launch mode (sending radar data to a passive radar-guided missile), or if the radar is that of an active radar-guided missile. In either of these cases, a distinctive missile launch tone is played and the pilot is advised to immediately act to counter the threat. Note that the RWR has no way of knowing if a heat-seeking missile is on its way to our aircraft.
Aside from radar, there are other technologies that are used to lock on to enemy aircraft and ground targets. A targeting pod is a very powerful camera mounted on an articulating swivel that allows it to look in nearly every direction. This camera is connected to image processor that is able to tell apart vehicles and buildings from surrounding terrain, and track moving targets. This is the SNIPER XR targeting pod:
How Fighter Jets Lock On (and How the Targets Know)
And this is what the pilot sees when he operates it:
How Fighter Jets Lock On (and How the Targets Know)
The pod is able to track vehicles day and night, using visual or infra-red cameras. Heat-seeking missiles obviously use this same technology to home in on aircraft, and electro-optical missiles use this technology to track ground targets.
Lastly, there are laser-guided missiles as well. These "beam riders" follow a laser beam emanating from the aircraft to the target. Many ground vehicles use laser rangefinders as well, and some aircraft include a laser warning system (LWS) that works similarly to an RWR, but displays incoming laser signals instead.