Hi, I'm new to diving so excuse this basic question.
If you're diving off of a boat and you have a flag out how do you know where that flag is after you've been swimming around down there?
Before you descend take a look at your compass and orientate yourself to the surrounding surface topography. What lies North? East? South? West? Is it all open ocean or are you anchored within close proximity of the land? If you can't find the boat or if the boat drifts or has to leave divers in the water, is there someplace that will be safe to surface or a possible alternate exit? If you're at sea with nothing but ocean around, as you descend, look at the bottom of your boat. Note the features. What kind of hull does it have? What color is it? One prop or two? THis way, should another boat anchor in the vicinity, you'll be able to identify yours underwater.
Next, begin your descent down the anchor line. Check your compass and verify which direction the anchor line is running, Note if there is a surface current. Currents can change while underwater, but most of the time the boat is pretty much facing the way you left it. When you reach 10, 15, or 20 feet, check the anchor line to see if there are any features in these depths that you can identify by touch. If you lose a mask and manage to ascend the anchor line, being able to feel something when you can't read your computer will allow you you to make your safety decompression stops. You can count time by thinking, "1 Mississippi ... 2 Misssissippi ... etc." Check equipment and buddy's status and buddy's equipment as you continue down.
Once you reach the bottom, verify that the anchor is secure. Note the features of the anchor so you can identify it, should another boat anchor nearby, you won't be confused. Look at the bottom features. Is there anything that will help you locate the anchor line? Any wreckage? Any rocks? Reef features or coral heads? Bottom contours? Anything that you can spot and that will help you navigate to the anchor line? Check your gear, that of your buddy and buddy your buddy's status. Shoot a compass bearing at whatever feature might help you and figure out its reciprocal. Is there a bottom current? How will that affect your course? Alternatively run a reel and line. Don't develop poor line habits. Find an experienced instructor who has been trained how to properly run line. Instructors who have taken cave courses or are cave instructors can teach you this skill. I've seen some incredibly poor instruction regarding the use of line by many instructors over the years.
If running a line, check your compass to back up your line work. If not running line, the easiest way to navigate is along a path that can easily be followed. A wall, a reef with a distinct "road" such as an undercut in the coral, moving from distinct object to distinct object and recalling their reverse order and direction are a few ways to take advantage of pilotage. Look back frequently and imagine your return path. Check your direction often and be aware of the time/distance relationship. Don't be in a hurry. The faster you swim with the least attention paid to your surroundings, compass and watch, the more likely you will get lost.
If using a compass, running patterns can often be helpful. An expanding square allows you to explore on all sides of the boat and learn the terrain around which the boat is anchored. As you get farther fro the boat, you note the features that you can identify once you can no longer see the boat or anchor line. If you are going to swim a linear course, noting the features on the bottom that will help you discover you are in the general area of home will give you a bigger target to locate when you return. Pay lots of attention. Plan to arrive back at the boat early and spend the rest of your time relaxing and enjoying the area around the boat before it is time to ascend. By returning early, you'll give yourself extra time to sort out navigational errors if you get lost. Swimming out and back patterns allow you to go explore and then find your way back to the boat, select another direction and head off then return. This keeps the dive interesting, keeps you closer to home and allows you to continually verify the location of the boat.
Additional items like strobe lights tied to the anchor, locator beacons, and such are also available.
Generally, good compass and line work are all you need.
If you get lost, ascend carefully looking around and listening for boat motors. Stay down if you hear anything cruising close to you. Once up, deploy a surface marker buoy if you choose not to return to the boat by swimming. If just taking a look, take a compass bearing and then descend and swim that heading back home.